bit operations


            see also operators - bitwise shift operators


  You cannot depict leading bits with the toBinaryString( ) method of the Integer wrapper class.  Yes it can show integer bits.  Unfortunately it does so without providing any leading zeroes.  i.e.


            byte x = 1;

                System.out.print( Integer.toBinaryString( x ) );           prints just 1, not the 00000001 which you might have wanted


  A quick rule to visually read a negative bit string as a decimal number is this:


            Three steps: (1) Flip the bits, (2) add in 1 more, then (3) read it as a (now negative) number.


i.e.  Let's read  1111 1111 1111 1001 which happens to represent minus seven.  The presently-zero 2 bit plus the presently-zero 4 bit both become one bits in the ensuing flip to 0000 0000 0000 0110, So that's 6 when the bits are flipped after step one  Then we add in 1 more, in the second step, thus making it 7, and thirdly we read it as a negative value.  That means you read it as a negative 7.


  A quick rule to visually convert a positive bit string to negative is this:


            Two steps: (1) Subtract one, then (2) flip all the bits.


i.e. This rule says positive 1 should become all ones if negative.  Let's see… Using a byte example, positive 0000 0001 becomes 0000 0000 in step one when 1 is subtracted.  Then it does indeed become 1111 1111 in step two when its bits are flipped.  And 1111 1111 is negative 1.