This is due to the expansion of the sign when performing a shift to the right.
0xA0 <=> 10100000 binary (and is a negative number because the MSB is set to 1)
(0xA0 >> 1) <=> 11010000
replace char cwith unsigned char c
or mask the MSB after the shift.
int main()
{
char c = 0x00;
c |= 0xA0;
while(c != 0x00)
{
cout << (c & 1) << endl;
c = (c >> 1) & 0x7f;
}
}
Note that charit is usually signed (range -128..127), but some c / C ++ compiler defines it charas unsigned(AIX xlC compiler is a known case).
source
share