I am trying to print the bits of a number. I am doing a logical 'AND' of 1 with the number to extract the bit. But I receive special characters.
Infact a basic left shift by 1 does not work. I am using borland compiler. This is my program and output I receive.
Please suggest what is wrong here.
#include <iostream.h>
#include <conio.h>
int main()
{
unsigned char x = 0;
unsigned char ONE = 1;
do
{
printf("Enter the number and press enter - 0 to exit\n");
cin>>x;
printf("Number before left shifting by 1: %c\n",x);
printf("Number after left shifting by 1: %c\n",x<<1);
for (int i=0;i<8 ;++i )
{
printf(" Bit [%d] of the number : %c\n", i, (x&ONE));
ONE = (ONE<<1);
}
}while(getche()!='0');
return 0;
}
R:\CPP>bit.exe
Enter the number and press enter - 0 to exit
1
Number before left shifting by 1: 1
Number after left shifting by 1: b
Bit [0] of the number : ☺
Bit [1] of the number :
Bit [2] of the number :
Bit [3] of the number :
Bit [4] of the number : ►
Bit [5] of the number :
Bit [6] of the number :
Bit [7] of the number :
R:\CPP>