Bit Manipulation: Binary Representation

Binary is a base-two numbering system. Each digit in a binary number is known as a bit, and a bit can either have a value of 0 or 1. Because computers have limited memory, binary numbers are typically restricted to an n-bit amount. An n-bit binary number can have different values depending on the encoding used. An unsigned n-bit number varies between 0 and (2n - 1), whereas a 2’s complement or signed n-bit number varies between -(2n-1) and (2n-1 - 1).

Due to the bit restrictions placed on binary numbers, it’s possible to perform mathematical operations on two numbers and get a result that falls outside the boundaries of the of the range the encoding can offer. If the result is too large, it is called overflow, and when the result is less than the lower boundary, it is called underflow. What usually happens in overflow is that the n-bit number needs more than n bits to be represented accurately. A similar thing happens for underflow. When you subtract 7 from 9, and you get a negative number, but you have an unsigned encoding where the smallest number is 0, and underflow has happened.

For examples in 2’s complement: 4-bit addition of 0xF and 0xF does not cause overflow since -1 + -1 = -2. 8-bit addition of 0x50 and 0x7F causes overflow. Since 0x7F is the largest possible number, adding any positive number to it will cause an overflow. Similarly, 16-bit addition of 0x8000 and 0xEFFF causes underflow. Because 0x8000 is the smallest negative number, adding any other negative number causes underflow. Finally, 32-bit addition of 0x5FFFFFFF and 0xF5555555 does not cause overflow. The first number is positive, and the second number is negative. The addition of such a pair never causes an overflow.

Because binary operations are limited by the number of bits allocated, it is essential to know the size of each data type to ensure overflow does not occur.

ask yourself