• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

having a tough time with bitwise operators...could somebody help??

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont understand the concept...how they work..
this is an example from a book :
if foo is an integer variable ,then
int fourthBitFromRight = (foo & 8) / 8 ;
gives you a one if the fourth bit from the right in the binary representation of foo is one , and a zero if not.
could you please explain what the above statement means??

could somebody also tell me how the other bitwise operators ie
| ("or") , ^ ("xor") and ~("not") work..
I did not understand these too.. >> , << , and >>>
what do they do actually??
could you also please tell me any website which has something about these,and some examples...
thanks in advance..
Vidya.
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vidya Sajeev:
I dont understand the concept...how they work..
this is an example from a book :
if foo is an integer variable ,then
int fourthBitFromRight = (foo & 8) / 8 ;
gives you a one if the fourth bit from the right in the binary representation of foo is one , and a zero if not.
could you please explain what the above statement means??

could somebody also tell me how the other bitwise operators ie
| ("or") , ^ ("xor") and ~("not") work..
I did not understand these too.. >> , << , and >>>
what do they do actually??
could you also please tell me any website which has something about these,and some examples...
thanks in advance..
Vidya.


Before any of these can be understood, you have to know how to convert a number to it's binary representation.(I'm not going to cover that)
8 represented in binary is 00001000.
& is a bitwise operator, it evalutes each operand bit by bit and sets the coresponding bit in the result when that bit is set in both operands. So, 255&8
255 = 11111111
8 = 00001000
since only the 4th bit from the right is set in both, only the 4th bit in the result gets set
result = 00001000 = 8
lets try 5
5 = 00000101
8 = 00001000
now in this example, none of the bits are set in both so
result = 00000000 = 0
Now | sets the bit in the result if it is set in either or both operands
255 = 11111111
8 = 00001000
result = 11111111 = 255
5 = 00000101
8 = 00001000
result = 00001101 = 13
^ sets the bit in the result if the bit is set in either operand but, not if it is set in both
255 = 11111111
8 = 00001000
result = 11110111 = 247
5 = 00000101
8 = 00001000
result = 00001101 = 13
~ is slightly diferent in that it operates on one operand only, it inverts every bit
255 = 11111111
~255 = 00000000
5 = 00000101
~5 = 11111010
8 = 00001000
~8 = 11110111

On to the shifts. Shifting moves the bits in the direction indicated, the number of places that you shift it.
<<<br /> 8<<2<br /> everything gets shifted 2 places to the left, the left bits get filled with 0<br /> 8<<2<br /> 8 = 00001000<br /> result = 00100000 = 32<br /> 5<<2<br /> 5 = 00000101<br /> result = 00010100 = 20<br /> 255<<2<br /> 255 = 11111111<br /> result 1111100 = 252<br /> right shifting has the 2 variants, signed >> and unsigned >>><br /> unsigned first becaaaaause I'm lazy<br /> 8>>>2<br /> 8 = 00001000<br /> result = 00000010 = 2<br /> 5>>>2
5 = 00000101
result = 00000001 = 1
255 >>>2
255 = 11111111
result = 00111111 = 65
As you notice, if a bit runs off the end, it's gone to electronic heaven never to be found again.
To this piont I've been simplifing things by working on a single byte. Now in java both operands must be of type int 4 bytes! in fact if they are of a smaller intergrel type , they are automatically converted to an int first and then the operation occurs so the example 255<<2 would result in a larger value<br /> 255 = 00000000 00000000 00000000 11111111<br /> 255 << 2<br /> result = 00000000 00000000 00000011 11111100<br /> I did that to prevent having to type all those zeros the consepts are factual only the size was changed to protect the fingers.<br /> That leaves signed right shift. byte, short, int are all signed datatypes if the leftmost bit is set, it represents a negative number so <br /> -1 = 11111111 11111111 11111111 11111111<br /> signed shifts happen in two steps if the number is originally negative<br /> first it's shift like the unsigned one<br /> -1>>2<br /> result = 00111111 11111111 11111111 11111111<br /> then the left most bit is reset and the result is again negative<br /> result = 10111111 11111111 11111111 11111111<br /> notice if you do a signed right shift on -1, one place<br /> -1 >> 1
result = 01111111 11111111 11111111 11111111
reset the negative
result = 11111111 11111111 11111111 11111111 ops: Still -1
I leave with one more important note shift are done by modulas math. By that I mean the number you shift by is first check by X%32
if you
5 >>> 34 means 5 >>> 34%32 or 5 >>> 2
a shift by 32
5 << 32 means 5 << 32%32 or 5<<0
I hope this helps
 
Vidya Sajeev
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A zillion thanks Carl, it really helped....
Vidya.
 
reply
    Bookmark Topic Watch Topic
  • New Topic