• 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
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Regarding bitwise operators

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
Throw me some info on bitwise operators
like >> , >>> , << .tell me with some
examples
thanks in advance
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The << operator causes the bits of the left operand to be shifted to the left, based on the value of the right operand. The new bits that are used to fill in the shifted right bits have the value 0. Figure A illustrates the process of shifting left. Note that the leftmost bits shifted out of the operand are discarded.<br /> The >> operator causes the bits of the left operand to be shifted to the right, based on the value of the right operand. The bits that fill in the shifted left bits have the value of the leftmost bit (before the shift operation). The >> operator is said to be a signed shift because it preserves the sign (positive or negative) of the operand. Figure B illustrates how the signed right shift is performed. The right-most bits that are shifted out of the operand are discarded.
The >>> operator is identical to the >> operator, except that the bits that fill in the shifted left bits have the value of 0. The >>> operator is said to be an unsigned shift because it does not preserve the sign of the operand.
Refer to JLS for more details.
 
bala_chocos
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please explain >> ,>>> ,<< with one example
 
Ranch Hand
Posts: 347
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
-5 >> 2 = -2
-5 >>> 2 = 1073741822
-5 << 2 = -20<br /> -5 >> 2 = -2
Shift bits right two places, filling in to the left with ones.
-5 decimal is 11111111 11111111 11111111 11111011 binary.
11111111 11111111 11111111 11111011 >> 2 is
11111111 11111111 11111111 11111110 binary which is -2 decimal
-5 >>> 2 = 1073741822
Same idea, but fill in bits shifted from left with zeros instead of ones.
-5 decimal is 11111111 11111111 11111111 11111011 binary.
11111111 11111111 11111111 11111011 >>> 2 is
00111111 11111111 11111111 11111110 binary which is
1073741822 decimal
-5 << 2 = -20<br /> Shift left (has same effect as multiplication).<br /> -5 decimal is 11111111 11111111 11111111 11111011 binary.<br /> 11111111 11111111 11111111 11111011 << 2 is<br /> 11111111 11111111 11111111 11101100 binary which is -20 decimal<br /> Small example program so you can see this is true:<br />
Hope this helps.
Stephanie
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marcus has a tutorial on bit-shift operators.
 
bala_chocos
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi stephen
thanks for valuable help and also for jply
Iam still having doubts . i understand the concepts
after shifting ,how are you converting binary numbers
to decimal .this may be silly doubt for everybody
please help me
reply
    Bookmark Topic Watch Topic
  • New Topic