2.11.2. Binary Output

Forward Reference Review

The example uses an unusual operator and a data type modifier introduced previously. Please review these as needed.

We'll learn later that C++ has many library or API functions for formatting output. There isn't anything special about these functions - they just convert numbers into characters or strings of characters. As an example of bitwise operators, we'll write a simple program to display an integer in binary, a string of bits.

Caution

Integers, including unsigned integers, span two to eight bytes of memory, and the underlying computer hardware determines their order. The solution presented here works on a little endian computer (e.g., Intel or AMD). It does not work on big endian hardware.

Solution

#include <iostream>
#include <string>
using namespace std;

int main()
{
	unsigned number;
	cout << "Please enter a number: ";
	cin >> hex >> number;

	for (int i = 8 * sizeof(int) - 1; i >= 0; i--)
		cout << ((number >> i) & 1);
	cout << endl;

	return 0;
}
Printing a number in binary.
hex
We can use the hex manipulator for both input and output. As used here, it reads input from the console in hexadecimal or base-16. We could input a number in decimal or base-10, but using hexadecimal makes the description in Figure 2 a little easier.
sizeof(int)
The underlying computer hardware and the compiler determine the size of an integer. The sizeof operator returns the number of bytes in an integer.
8 * sizeof(int) - 1;
Multiplying the number of bytes in an integer by 8 gives the number of bits in an integer. The program subtracts 1 because the bits are numbered or counted starting at 0 - from 0 to 31 for a 32-bit integer.
...; i >= 0; i--
Together, these two parts of a for-loop run the loop backward from 31 to 0 (including 0). The ellipses denote code removed for clarity.
number >> i
Shifts the bits the variable number i bits to the right. The first time through the loop, it shifts the bits 31 places, leaving only the highest bit in position 0; the last time through the loop, it sifts the bits 0 places.
(number >> i) & 1
The bitwise-AND with 1 "masks out" or changes to 0 all the bits except the bit at position 0, which is unchanged.
   2    2    2    1    1               
   8    4    0    6    2    8    4    0
1010 0000 1011 0000 1100 0000 1101 0000
  a    0    b    0    c    0    d    0
(a)
For i = 4
  1010 0000 1011 0000 1100 0000 1101 0000 >> 4
= 0000 1010 0000 1011 0000 1100 0000 1101
(b)
  0000 1010 0000 1011 0000 1100 0000 1101
&
  0000 0000 0000 0000 0000 0000 0000 0001
=
  0000 0000 0000 0000 0000 0000 0000 0001
(c)
Printing in binary example. Let's imagine that we run the above program and enter the hexadecimal number a0b0c0d0 (you may use upper or lower case letters). To create a test case and to make it easier for us to follow the operations that take place in the body of the for-loop, we manually convert the hexadecimal number to binary. If we use the table shown in Figure 5, the conversion is straightforward.
  1. The input number in both binary and hexadecimal. We break the binary number into 4-bit groups or nibbles to make it easier to read. The numbers highlighted in yellow are the bit positions - most significant to the left and least significant to the right.
  2. The for-loop runs through 32 iterations, from i = 31 through i = 0. (b) illustrates how the bit-shift operation affects the value in number when i = 4.
  3. The parentheses cause the bit-shift operation to occur first. (c) illustrates the bitwise-AND with 1 (displayed in binary as 31 leading 0s and one 1-bit). The bitwise-AND operation masks out (0 & x = 0) the left-most 31 bits; the right-most bit is passed through (1 & x = x).