lib/standard/: Removed bin_and/or/xor/not from math
authorLucas Bajolet <r4pass@hotmail.com>
Fri, 7 Aug 2015 20:27:58 +0000 (16:27 -0400)
committerLucas Bajolet <r4pass@hotmail.com>
Mon, 10 Aug 2015 15:01:10 +0000 (11:01 -0400)
Signed-off-by: Lucas Bajolet <r4pass@hotmail.com>

12 files changed:
examples/rosettacode/bitwise_operations.nit
examples/rosettacode/hailstone.nit
examples/rosettacode/perlin_noise.nit
lib/bcm2835/bcm2835.nit
lib/bitmap/bitmap.nit
lib/markdown/markdown.nit
lib/noise.nit
lib/perfect_hashing.nit
lib/standard/math.nit
lib/standard/re.nit
src/interpreter/naive_interpreter.nit
tests/test_binariesop.nit

index c24ee4e..d543ad5 100644 (file)
@@ -5,15 +5,14 @@
 
 # Task: Bitwise operations
 # SEE: <http://rosettacode.org/wiki/Bitwise_operations>
-# Without logical right shift
 module bitwise_operations
 
 fun bitwise(a, b: Int)
 do
-       print "a and b: { a.bin_and(b) }"
-       print "a or b: { a.bin_or(b) }"
-       print "a xor b: { a.bin_xor(b) }"
-       print "not a: { a.bin_not }"
+       print "a and b: { a & b }"
+       print "a or b: { a | b }"
+       print "a xor b: { a ^ b }"
+       print "not a: { ~a }"
        print "a << b: { a << b }"
        print "a >> b: { a >> b }"
 end
index b76c533..af68297 100644 (file)
@@ -25,7 +25,7 @@ fun hailstone (n: Int): Array[Int]
 do
        var sequence = [n]
        while n != 1 do
-               if n.bin_and(0x01) == 0 then
+               if n & 0x01 == 0 then
                        n = n / 2
                else
                        n = 3 * n + 1
index a3352da..0733bbf 100644 (file)
@@ -33,9 +33,9 @@ class ImprovedNoise
        # Noise value in [-1..1] at 3d coordinates `x, y, z`
        fun noise(x, y, z: Float): Float
        do
-               var xx = x.to_i.bin_and(255)
-               var yy = y.to_i.bin_and(255)
-               var zz = z.to_i.bin_and(255)
+               var xx = x.to_i & 255
+               var yy = y.to_i & 255
+               var zz = z.to_i & 255
 
                x -= x.floor
                y -= y.floor
@@ -65,10 +65,10 @@ class ImprovedNoise
        # Value at a corner of the grid
        fun grad(hash: Int, x, y, z: Float): Float
        do
-               var h = hash.bin_and(15)
+               var h = hash & 15
                var u = if h < 8 then x else y
                var v = if h < 4 then y else if h == 12 or h == 14 then x else z
-               return (if h.is_even then u else -u) + (if h.bin_and(2) == 0 then v else -v)
+               return (if h.is_even then u else -u) + (if h & 2 == 0 then v else -v)
        end
 end
 
index 99fd4a4..a7f9d69 100644 (file)
@@ -213,15 +213,15 @@ class HD44780
        do
                var fs = flag_function_set
                if bits == 8 then
-                       fs = fs.bin_or(16)
+                       fs = fs | 16
                else if bits != 4 then abort
 
                if lines == 2 then
-                       fs = fs.bin_or(8)
+                       fs = fs | 8
                else if lines != 1 then abort
 
                if dots_wide == 10 then
-                       fs = fs.bin_or(4)
+                       fs = fs | 4
                else if dots_wide != 8 then abort
 
                write(true, fs)
@@ -231,17 +231,11 @@ class HD44780
        do
                var fs = flag_display_control
 
-               if on then
-                       fs = fs.bin_or(flag_display_on)
-               else fs = fs.bin_or(flag_display_off)
+               fs |= if on then flag_display_on else flag_display_off
 
-               if cursor then
-                       fs = fs.bin_or(flag_cursor_on)
-               else fs = fs.bin_or(flag_cursor_off)
+               fs |= if cursor then flag_cursor_on else flag_cursor_off
 
-               if blink then
-                       fs = fs.bin_or(flag_blink_on)
-               else fs = fs.bin_or(flag_blink_off)
+               fs |= if blink then flag_blink_on else flag_blink_off
 
                write(true, fs)
        end
@@ -250,13 +244,9 @@ class HD44780
        do
                var fs = flag_entry_mode_set
 
-               if left then
-                       fs = fs.bin_or(flag_entry_left)
-               else fs = fs.bin_or(flag_entry_right)
+               fs |= if left then flag_entry_left else flag_entry_right
 
-               if incr then
-                       fs = fs.bin_or(flag_entry_shift_increment)
-               else fs = fs.bin_or(flag_entry_shift_decrement)
+               fs |= if incr then flag_entry_shift_increment else flag_entry_shift_decrement
 
                write(true, fs)
        end
@@ -349,7 +339,7 @@ class HD44780
                var lb = once [1,2,4,8]
                for i in [0..4[ do
                        var b = lb[i]
-                       var r = b.bin_and(v) != 0
+                       var r = b & v != 0
                        var d = ds[i]
                        d.write(r)
                end
@@ -384,7 +374,7 @@ class HD44780
                var hb = once [16,32,64,128]
                for i in [0..4[ do
                        var b = hb[i]
-                       var r = b.bin_and(cmd) != 0
+                       var r = b & cmd != 0
                        var d = ds[i]
                        d.write(r)
                end
@@ -403,7 +393,7 @@ class HD44780
                var lb = once [1,2,4,8]
                for i in [0..4[ do
                        var b = lb[i]
-                       var r = b.bin_and(cmd) != 0
+                       var r = b & cmd != 0
                        var d = ds[i]
                        d.write(r)
                end
index 47475e7..e408402 100644 (file)
@@ -199,10 +199,10 @@ class Bitmap
        # type, Int is used.
        private fun set_value(array: Array[Int], start_index: Int, value: Int)
        do
-               array[start_index] = value.bin_and(0x000000FF)
-               array[start_index + 1] = (value >> 8).bin_and(0x000000FF)
-               array[start_index + 2] = (value >> 16).bin_and(0x000000FF)
-               array[start_index + 3] = (value >> 24).bin_and(0x000000FF)
+               array[start_index] = value & 0x000000FF
+               array[start_index + 1] = (value >> 8) & 0x000000FF
+               array[start_index + 2] = (value >> 16) & 0x000000FF
+               array[start_index + 3] = (value >> 24) & 0x000000FF
        end
 
        # Saves the bitmap into a file
@@ -224,8 +224,8 @@ class Bitmap
                        for y in [0..self.width[ do
                                var pixel = row[y]
                                var red = pixel >> 16
-                               var green = pixel.bin_and(0x00FF00) >> 8
-                               var blue = pixel.bin_and(0x000000FF)
+                               var green = (pixel & 0x00FF00) >> 8
+                               var blue = pixel & 0x000000FF
                                fw.write(red.ascii.to_s)
                                fw.write(green.ascii.to_s)
                                fw.write(blue.ascii.to_s)
@@ -242,8 +242,8 @@ class Bitmap
                        for y in [0..self.width[ do
                                var pixel = row[y]
                                var red = pixel >> 16
-                               var green = pixel.bin_and(0x00FF00) >> 8
-                               var blue = pixel.bin_and(0x000000FF)
+                               var green = (pixel & 0x00FF00) >> 8
+                               var blue = pixel & 0x000000FF
                                var lum = (0.2126 * red.to_f + 0.7152 * green.to_f + 0.0722 * blue.to_f).to_i
                                pixel = lum * 256 * 256 + lum * 256 + lum
                                self.data[x][y] = pixel
index 854eed5..5677efd 100644 (file)
@@ -169,7 +169,7 @@ class MarkdownProcessor
                                if c == '\n' then
                                        eol = true
                                else if c == '\t' then
-                                       var np = pos + (4 - (pos.bin_and(3)))
+                                       var np = pos + (4 - (pos & 3))
                                        while pos < np do
                                                value.add ' '
                                                pos += 1
index 1a0d7ce..9a1ecc0 100644 (file)
@@ -356,6 +356,6 @@ redef universal Int
        # The missing 2 bits are used to tag integers by the Nit system.
        private fun mask: Int
        do
-               return bin_and(0x3FFF_FFFF)
+               return self & 0x3FFF_FFFF
        end
 end
index 38808cb..d8ab3cb 100644 (file)
@@ -52,11 +52,11 @@ class Perfecthashing
                var orMask = 0
                var andMask = 0
                for i in ids do
-                       orMask = orMask.bin_or(i)
-                       andMask = andMask.bin_and(i)
+                       orMask |= i
+                       andMask &= i
                end
 
-               mask = orMask.bin_xor(andMask)
+               mask = orMask ^ andMask
 
                # Re-initialize the hashtable with null values
                for i in [0..(mask+1)] do tempht[i] = null
@@ -66,7 +66,7 @@ class Perfecthashing
                var i = mask.highest_bit
                while i != 0 do
                        if mask.getbit(i) == 1 then
-                               newmask = mask.bin_xor(1 << i)
+                               newmask = mask ^ (1 << i)
 
                                # If there is no collision, replace the old mask
                                if phandp(ids, newmask) then
@@ -85,7 +85,7 @@ class Perfecthashing
        fun phandp(ids: Array[Int], mask: Int): Bool
        do
                for i in ids do
-                       var hv = i.bin_and(mask)
+                       var hv = i & mask
                        if tempht[hv] == mask then
                                return false
                        else
@@ -156,7 +156,7 @@ class Perfecthashing
                        var i = inter.item.first.as(not null)
                        while i != inter.item.second and not found do
                                # Tests if this id is free for this mask
-                               var hv = i.bin_and(mask)
+                               var hv = i & mask
 
                                # If the hashtable if full, push an empty item
                                if hv >= tempht.length then
index 3e02ebe..50b36e9 100644 (file)
@@ -66,35 +66,23 @@ redef class Int
 
        # Returns the result of a binary AND operation on `self` and `i`
        #
-       #     assert 0x10.bin_and(0x01) == 0
-       fun bin_and(i: Int): Int `{ return self & i; `}
-
-       # Alias of `bin_and`
-       fun &(i: Int): Int do return bin_and(i)
+       #     assert 0x10 & 0x01 == 0
+       fun &(i: Int): Int `{ return self & i; `}
 
        # Returns the result of a binary OR operation on `self` and `i`
        #
-       #     assert 0x10.bin_or(0x01) == 0x11
-       fun bin_or(i: Int): Int `{ return self | i; `}
-
-       # Alias of `bin_or`
-       fun |(i: Int): Int do return bin_or(i)
+       #     assert 0x10 | 0x01 == 0x11
+       fun |(i: Int): Int `{ return self | i; `}
 
        # Returns the result of a binary XOR operation on `self` and `i`
        #
-       #     assert 0x101.bin_xor(0x110) == 0x11
-       fun bin_xor(i: Int): Int `{ return self ^ i; `}
-
-       # Alias of `bin_xor`
-       fun ^(i: Int): Int do return bin_xor(i)
+       #     assert 0x101 ^ 0x110 == 0x11
+       fun ^(i: Int): Int `{ return self ^ i; `}
 
        # Returns the 1's complement of `self`
        #
-       #     assert 0x2F.bin_not == -48
-       fun bin_not: Int `{ return ~self; `}
-
-       # Alias of `bin_not`
-       fun ~: Int do return bin_not
+       #     assert ~0x2F == -48
+       fun ~: Int `{ return ~self; `}
 
        # Returns the square root of `self`
        #
@@ -114,8 +102,8 @@ redef class Int
                if o < 0 then return -(self.gcd(-o))
                if self == 0 or o == self then return o
                if o == 0 then return self
-               if self.bin_and(1) == 0 then
-                       if o.bin_and(1) == 1 then
+               if self & 1 == 0 then
+                       if o & 1 == 1 then
                                return (self >> 1).gcd(o)
                        else
                                return (self >> 1).gcd(o >> 1) << 1
@@ -186,35 +174,23 @@ end
 redef class Byte
        # Returns the result of a binary AND operation on `self` and `i`
        #
-       #     assert 0x10.bin_and(0x01) == 0
-       fun bin_and(i: Byte): Byte `{ return self & i; `}
-
-       # Alias of `bin_and`
-       fun &(i: Byte): Byte do return bin_and(i)
+       #     assert 0x10u8 & 0x01u8 == 0u8
+       fun &(i: Byte): Byte `{ return self & i; `}
 
        # Returns the result of a binary OR operation on `self` and `i`
        #
-       #     assert 0x10.bin_or(0x01) == 0x11
-       fun bin_or(i: Byte): Byte `{ return self | i; `}
-
-       # Alias of `bin_or`
-       fun |(i: Byte): Byte do return bin_or(i)
+       #     assert 0x10u8 | 0x01u8 == 0x11u8
+       fun |(i: Byte): Byte `{ return self | i; `}
 
        # Returns the result of a binary XOR operation on `self` and `i`
        #
-       #     assert 0x101.bin_xor(0x110) == 0x11
-       fun bin_xor(i: Byte): Byte `{ return self ^ i; `}
-
-       # Alias of `bin_xor`
-       fun ^(i: Byte): Byte do return bin_xor(i)
+       #     assert 0x101u8 ^ 0x110u8 == 0x11u8
+       fun ^(i: Byte): Byte `{ return self ^ i; `}
 
        # Returns the 1's complement of `self`
        #
-       #     assert 0x2F.bin_not == -48
-       fun bin_not: Byte `{ return ~self; `}
-
-       # Alias of `bin_not`
-       fun ~: Byte do return bin_not
+       #     assert ~0x2Fu8 == 0xD0u8
+       fun ~: Byte `{ return ~self; `}
 end
 
 redef class Float
index 1f488ad..7d588ca 100644 (file)
@@ -205,10 +205,10 @@ class Regex
        fun compile: nullable Error
        do
                var cflags = 0
-               if extended then cflags = cflags.bin_or(flag_extended)
-               if ignore_case then cflags = cflags.bin_or(flag_icase)
-               if optimize_is_in then cflags = cflags.bin_or(flag_nosub)
-               if newline then cflags = cflags.bin_or(flag_newline)
+               if extended then cflags |= flag_extended
+               if ignore_case then cflags |= flag_icase
+               if optimize_is_in then cflags |= flag_nosub
+               if newline then cflags |= flag_newline
 
                var native = self.native
                var need_compilation = native == null or cflags != cflags_cache or string != string_cache
@@ -264,8 +264,8 @@ class Regex
        private fun gather_eflags: Int
        do
                var eflags = 0
-               if not_bol then eflags = eflags.bin_or(flag_notbol)
-               if not_eol then eflags = eflags.bin_or(flag_noteol)
+               if not_bol then eflags |= flag_notbol
+               if not_eol then eflags |= flag_noteol
                return eflags
        end
 
@@ -401,7 +401,7 @@ class Regex
                text = text.to_s
                var cstr = text.to_cstring
                var eflags = gather_eflags
-               var eflags_or_notbol = eflags.bin_or(flag_notbol)
+               var eflags_or_notbol = eflags | flag_notbol
                var native_match = self.native_match
                var matches = new Array[Match]
 
index c8fc863..3d3aa12 100644 (file)
@@ -903,21 +903,13 @@ redef class AMethPropdef
                                return v.float_instance(recvval.to_f)
                        else if pname == "to_b" then
                                return v.byte_instance(recvval.to_b)
-                       else if pname == "lshift" then
-                               return v.int_instance(recvval.lshift(args[1].to_i))
-                       else if pname == "rshift" then
-                               return v.int_instance(recvval.rshift(args[1].to_i))
+                       else if pname == "<<" then
+                               return v.int_instance(recvval << args[1].to_i)
+                       else if pname == ">>" then
+                               return v.int_instance(recvval >> args[1].to_i)
                        else if pname == "rand" then
                                var res = recvval.rand
                                return v.int_instance(res)
-                       else if pname == "bin_and" then
-                               return v.int_instance(recvval.bin_and(args[1].to_i))
-                       else if pname == "bin_or" then
-                               return v.int_instance(recvval.bin_or(args[1].to_i))
-                       else if pname == "bin_xor" then
-                               return v.int_instance(recvval.bin_xor(args[1].to_i))
-                       else if pname == "bin_not" then
-                               return v.int_instance(recvval.bin_not)
                        end
                else if cname == "Byte" then
                        var recvval = args[0].to_b
@@ -949,10 +941,10 @@ redef class AMethPropdef
                                return v.float_instance(recvval.to_f)
                        else if pname == "to_i" then
                                return v.int_instance(recvval.to_i)
-                       else if pname == "lshift" then
-                               return v.byte_instance(recvval.lshift(args[1].to_i))
-                       else if pname == "rshift" then
-                               return v.byte_instance(recvval.rshift(args[1].to_i))
+                       else if pname == "<<" then
+                               return v.byte_instance(recvval << args[1].to_i)
+                       else if pname == ">>" then
+                               return v.byte_instance(recvval >> args[1].to_i)
                        else if pname == "byte_to_s_len" then
                                return v.int_instance(recvval.to_s.length)
                        end
index 3661e49..9806e3d 100644 (file)
@@ -1,9 +1,6 @@
 var a = 5
 var b = 5
 
-print a.bin_and(b) # 5
-print a.bin_or(b)  # 5
-print a.bin_xor(b) # 0
 print a & b
 print a | b
 print a ^ b
@@ -11,25 +8,17 @@ print a ^ b
 a = 0
 b = 5
 
-print a.bin_and(b) # 0
-print a.bin_or(b)  # 5
-print a.bin_xor(b) # 5
 print a & b
 print a | b
 print a ^ b
 
-print a.bin_not
 print ~a
 
 var c = 5u8
 var d = 5u8
 
-print c.bin_and(d) # 0
-print c.bin_or(d)  # 5
-print c.bin_xor(d) # 5
 print c & d
 print c | d
 print c ^ d
 
-print c.bin_not
 print ~c