+
+ # Alias of `bin_xor`
+ fun ^(i: Int): Int do return bin_xor(i)
+
+ # Returns the 1's complement of `self`
+ #
+ # assert 0x2F.bin_not == -48
+ fun bin_not: Int is extern "kernel_Int_Int_binnot_0"
+
+ # Alias of `bin_not`
+ fun ~: Int do return bin_not
+
+ # Returns the square root of `self`
+ #
+ # assert 16.sqrt == 4
+ fun sqrt: Int `{ return sqrt(self); `}
+
+ # Returns the greatest common divisor of `self` and `o`
+ #
+ # assert 54.gcd(24) == 6
+ # assert -54.gcd(-24) == 6
+ # assert 54.gcd(-24) == -6
+ # assert -54.gcd(24) == -6
+ # assert 12.gcd(6) == 6
+ fun gcd(o: Int): Int
+ do
+ if self < 0 then return -(-self).gcd(o)
+ 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
+ return self.rshift(1).gcd(o)
+ else
+ return self.rshift(1).gcd(o.rshift(1)).lshift(1)
+ end
+ end
+ if o.bin_and(1) == 0 then return self.gcd(o.rshift(1))
+ if self > o then return (self - o).rshift(1).gcd(o)
+ return (o - self).rshift(1).gcd(self)
+ end
+
+ # Is `self` even ?
+ #
+ # assert 12.is_even
+ fun is_even: Bool do return self % 2 == 0
+
+ # Is `self` odd ?
+ #
+ # assert not 13.is_even
+ fun is_odd: Bool do return not is_even
+
+ # Is self a prime number ?
+ #
+ # assert 3.is_prime
+ # assert not 1.is_prime
+ # assert not 12.is_prime
+ fun is_prime: Bool
+ do
+ if self == 2 then
+ return true
+ else if self <= 1 or self.is_even then
+ return false
+ end
+ for i in [3..self.sqrt[ do
+ if self % i == 0 then return false
+ end
+ return true
+ end
+
+ # Returns the `self` raised to the power of `e`.
+ #
+ # assert 2 ** 3 == 8
+ fun **(e: Int): Int
+ do
+ return self.to_f.pow(e.to_f).to_i
+ end
+
+ # The factorial of `self` (aka `self!`)
+ #
+ # Returns `1 * 2 * 3 * ... * self-1 * self`
+ #
+ # assert 0.factorial == 1 # by convention for an empty product
+ # assert 1.factorial == 1
+ # assert 4.factorial == 24
+ # assert 9.factorial == 362880
+ fun factorial: Int
+ do
+ assert self >= 0
+ var res = 1
+ var n = self
+ while n > 0 do
+ res = res * n
+ n -= 1
+ end
+ return res
+ end
+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)
+
+ # 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)
+
+ # 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)
+
+ # 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