i dont know >>= is



İn the code below ( double has 8 bytes) double 's 8 byte is shown in bytes.

But i cant understand for(i = 128; i; i >>= 1) what this is.

And i cant understand why we multiply double number with 128 or else.

Help me ..




#include <iostream>
using namespace std;

union bits {
bits(double n);
void show_bits();
double d;
unsigned char c[sizeof(double)];
};

bits::bits(double n)
{
d = n;
}

void bits::show_bits()
{
int i, j;

for(j = sizeof(double)-1; j>=0; j--) {
cout << "Bit pattern in byte " << j << ": ";
for(i = 128; i; i >>= 1)
if(i & c[j]) cout << "1";
else cout << "0";
cout << "\n";
}
}

int main()
{
bits ob(1991.829);

ob.show_bits();

return 0;
}
>>= is a left right shift assignment operator. It takes that value stored in i and shifts all it's bits to the left right 1 place (with the rightleftmost bit being set to 0)

It would be the same as writing i = i >> 1.

Info on operators: http://cplusplus.com/doc/tutorial/operators/

Left shift and right shift operators are not the same as insertion and extraction operators, even though they use the same syntax.
Last edited on
right shift NOT left shift
Doh! That was stupid of me.
In this case i is an int therefore the right shift will shift the sign bit which happens to be zero in this case. If the number were negative than the sign bit would have been 1.

That is a really bizarre piece of source code you've got there. Please use code tags next time you post.
That code is just a method of outputting the binary representation of the data.

i = 128 in binary is 10000000.
Each shift moves the 1 bit to the right one place.
At each value, i is ANDed with c[j]. The result will be 0 if that bit of c[j] is 0, and 1 if that bit of c[j] is 1, and the characters 1 or 0 are output accordingly.
Thanks a lot to all...
Topic archived. No new replies allowed.