• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Pls explain how bitwise operation is performed in java.

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please explain in details, how this operation is done.

long a = l >> 33;
int b = l1 >> 33;
long c = l >> 33;
Thanks
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A) long a = l >> 33;
B) int b = lL >> 33;
C) long c = l >> 33;
X = Y shift operation Z
Per the JLS, Java looks at the data of the left side of the operation, the Y. If it is a data type smaller than an int, it is cast to an int. Then, if it is an int as it is in A and C above, the right side of the operation is & (anded) with 0x1F or 31. If the left side of the shift operation is a long, the right side is & with 0x3F or 63.
So, A and C the left side of the shift is an int, the right side is & with 31
33 = 00000000 00000000 00000000 00100001
31 = 00000000 00000000 00000000 00011111
Which equals 1
In B the left side of the shift is a long so the right side is & with 63
63 = 00000000 00000000 00000000 00111111
Which remains 33
In all cases, the shift results in moving the only bit set off and leaving 0.

------------------
Hope This Helps:)
Carl Trusiak
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
alternative to "&", we could say that the right operand is "%" with the size of left operand. E.g. for int>>33, only shift 33%32=1 bit.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic