# 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
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
# 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
# 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
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)
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
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
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
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
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
# 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
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)
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
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
# 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
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
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
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
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
# 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`
#
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
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
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
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
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]
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
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
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
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