by Harry Tanama
How to Print Decimal Number in Binary (using C++) (Advance Topic)
using for loop.
unsigned int bit = num >> i;
bit = bit & 1;
std::cout << bit;
We need to loop the above statement depending on how many bits you want to print on the screen 8 bits, 16 bits, 32 bits, etc.
unsigned int bit = (num >> i ) & 1;
">>" means shifting right by 1 bit will divide by two.
i = how many positions to shift (this will be the loop counter).
& is AND bitwise operator to manipulate two bits (at least comparing 2 bits).
For example, decimal 8. Human number system is decimal (base 10) and computer number system is binary (base 2)
Look at the picture to understand the binary base 2 system.
decimal 8 = binary 1 0 0 0
When we do this statement (decimal >> 1), the computer does not read 8 as decimal number, the computer number system is base 2 or binary, the 8 become 1000. The computer knows this symbol ">>" is shifting right by 1 bit will divide by two.
We will print in 4 bits 0000 not 8 bits 0000 0000 (1byte).
So we set the counter i to 3 and decrement the counter by 1 as we loop this through our for loop with condition statement i >= 0 (inclusive zero because we want to get the index zero, we have to remember that we count from 0 not 1).
unsigned int num = 8;
for (unsigned int i = 3; i >= 0; - i)
{
unsigned int bit = num >> i;
bit = bit & 1;
std::cout << bit;
}
bit = num >> i;
First, we are right shifting 3 bit positions to the right from number 8 (1000), just remember we are shifting all the bits one and zero (not just shifting number 1), so after the right shift, bit will become 1 in decimal but in computer it becomes 0001.
Next we perform the bitwise AND '&' operation comparing bit 1000 to 0001 to get the first digit of bit to print to the screen.
bit = bit & 1;
1 0001
1 0001 &
- - - - - - -
0001 bit = 1
then we print the bit to the screen std::cout << bit;
It will display 1 on the screen.
Now i = 2 because i=3 decrement by 1 ( - i).
for (unsigned int i = 3; i >= 0; - i)
{
unsigned int bit = num >> i;
bit = bit & 1;
std::cout << bit;
}
Next, we do another right shift by 2 bit positions on number 8 (1000), it will become 0010 (in decimal is 2) and then we will perform the bitwise AND '&' operation comparing number 2 (0010) with 1 (0001) to get the next digit of bit to print to the screen.
0010
0001 &
- - - - - - -
0000 bit = 0
And then we print the bit to the screen std::cout << bit;
it will display 1 0 on the screen.
Now i = 1 because i=2 decrement by 1 ( - i).
for (unsigned int i = 3; i >= 0; - i){
unsigned int bit = num >> i;
bit = bit & 1;
std::cout << bit;
}
Next we do another right shift by 1 bit position to number 8 (1000) this will become 0100 (in decimal 4) and we will perform the bitwise AND '&' operation comparing 0100 to the bit 0001.
0100
0001 &
- - - - - - -
0000 bit = 0
then we print bit to the screen std::cout << bit;
it will display 1 0 0 on the screen.
Now i = 0 because i=1 decrement by 1 ( - i).
for (unsigned int i = 3; i >= 0; - i){
unsigned int bit = num >> i;
bit = bit & 1;
std::cout << bit;
}
int bit = num >> i;
Next we do another right shift by 0 bit position to number 8 (1000) this will become number 1000 (in decimal 8) and we will perform the bitwise AND '&' operation comparing 1000 to the bit 0001.
1000
0001 &
- - - - - - -
0000 bit = 0
then we print the bit to the screen, std::cout << bit;
it will display 1 0 0 0 on the screen.
That is how we print decimal to binary to the computer screen.
If you want to display 8 bits like this 0001 0000 (in decimal 16),
for (unsigned int i = 8; i >= 0; - i){
unsigned int bit = num >> i;
bit = bit & 1;
std::cout << bit;
}
you can use the counter i = 8 like counting the index of an array or vector and decrement the index by one, right shift every bit position and then compare the number with AND & bitwise operator to bit 0001 to get the digit of the bit either one or zero.
If you want to display 32 bits, like:
0000 0000 0000 0000 1000 0000 0000 0000 (in decimal 32,768)
you can use the counter i = 32 like counting the index of an array or vector. Or 64 bits and so on.
Top comments (0)