For a program utilising bitmasks I desired to write numbers in binary... i.e To copy the first 8 bits of x
to z
, I write
y = 0xff000000;
z = 0;
z = (y & x) | z
Where x, y, z
are all int
. Now using left shift and right shift operators I wanted to move 1s of y
right or left to bitmask another set of bits, so I write the following code
cout<< bitset<32>(y>>10) <<"\n" << bitset<32>(y<<10) <<endl;
Now what I expected as output was:
00000000001111111100000000000000
00000000000000000000000000000000
but I got:
11111111111111111100000000000000
00000000000000000000000000000000
- Why are the new bits '1' intead of '0' on first line of output?
- How can I change current output to desired output?