Jump to content

basic bitwise operation


SchweppesAle

Recommended Posts

~ reverses all bits. So 0 in 8 bits is... 00000000 (binary) or 0x00 (hex)

and ~0 is the reverse of that, which is... 11111111 (binary) or 0xff (hex)

this number, 0xff, is -1 in binary.

 

To find out why negative numbers are represented in this way (why is -1 0xff and not 10000001//0x81?) you can read up on two's complement. The main reason for this binary representation comes down to making it much easier to represent negative numbers at a lower level. Eg, subtraction is simply addition of a negative number, addition of negative numbers can be done without special cases in the same circuitry as addition of positive numbers, etc.

  • 3 weeks later...

~ reverses all bits. So 0 in 8 bits is... 00000000 (binary) or 0x00 (hex)

and ~0 is the reverse of that, which is... 11111111 (binary) or 0xff (hex)

this number, 0xff, is -1 in binary.

 

To find out why negative numbers are represented in this way (why is -1 0xff and not 10000001//0x81?) you can read up on two's complement. The main reason for this binary representation comes down to making it much easier to represent negative numbers at a lower level. Eg, subtraction is simply addition of a negative number, addition of negative numbers can be done without special cases in the same circuitry as addition of positive numbers, etc.

 

so with two's complimentary, I would calculate the numerical value of a binary string the normal way unless the most significant(left most?) bit is equal to 1.  Is that correct?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.