Binary Numbers and Other Number Systems

Power of Two

Given a number x. Determine whether x is a power of two.

Algorithm:

Bitwise AND of x and (x - 1) should return 0.

Explanation and Example:

Bitwise AND between two numbers returns 1 only in positions where both numbers have 1 in the corresponding bits. Consider various examples.

a) Power of two

In general, if \(x = 2^n\), then in binary representation x looks like \(1000...0\) (one bit 1 and n zeros). When we subtract 1 from a power of two, we get a number in which all bits to the right (less significant) of the set bit become 1, and the set bit itself becomes 0. Now consider the bitwise AND between x and (x - 1): For \(x = 2^n\):

The result is 0.

b) Other numbers

Now consider what happens if x is not a power of two:

If x has more than one bit set to 1, then subtracting 1 will not give all zeros in the result of the bitwise AND. For example: Let x = 6 (in binary: \(0110_2\)):

Estimation:

Code:

def isPowerOfTwo(x: Int): Boolean = (x & (x - 1)) == 0

isPowerOfTwo(16) // true

Counting Bit Set Bits

Given two numbers, x and y. Determine whether they contain the same number of ones in their bit representation.

Algorithm:

Example:

Consider the calculation example for the pair (6, 3):

Thus, for the pair (6, 3) the algorithm will return true.

Explanation:

Estimation:

Code:

@annotation.tailrec
def isSnoob(x: Int, y: Int): Boolean =
  if x == 0 && y == 0 then true
  else if x == 0 || y == 0 then false
  else if x % 2 == 1 && y % 2 == 0 then isSnoob(x, y >> 1)
  else if x % 2 == 0 && y % 2 == 1 then isSnoob(x >> 1, y)
  else isSnoob(x >> 1, y >> 1)

isSnoob(11, 13)
// true: "1101", "1011"

Converting Decimal to Binary

Given a number in decimal notation. Convert this number to binary.

Algorithm:

Conversion algorithm:

Example:

Consider the example of computation using the given algorithm, starting with the integer 13.

Thus, the binary equivalent number for the integer 13 using the given algorithm is (1101).

Explanation:

For any number the following holds: \(a = \left\lfloor \frac{a}{2} \right\rfloor \times 2 + (a \mod 2)\). Therefore, the binary representation of a number equals the binary representation of half the number with the remainder of dividing the number by 2 appended.

Estimation:

Code:

def decToBinConv(x: Int): String =
  val seqOfDivByTwo = Iterator.iterate(x)(a => a / 2)
  val binList = seqOfDivByTwo
    .takeWhile(a => a > 0)
    .map(a => a % 2)
  binList.mkString.reverse

decToBinConv(8) // "1000"
decToBinConv(7) // "111"

Palindrome

A palindrome is a number, word, or text that reads the same forwards and backwards.

Given a number x. Determine whether this number is a palindrome in a given number system.

Algorithm:

Example:

Given the number 12321.

Explanation:

Estimation:

Code:

def isPalindrome(number: Long): Boolean = isPalindrome(number, 10)

def isPalindrome(number: Long, base: Int): Boolean =
  number == 0 || number > 0 && number % base != 0 && {
    def loop(k: Long, r: Long): Boolean =
      if k <= r then k == r || k == r / base
      else loop(k / base, r * base + k % base)
    loop(number, 0L)
  }

isPalindrome(12321) // true

Links: