• 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

Bitwise Operators

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having great trouble in understanding bitwise operators,does anyone know a good tutorial for it?
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
i think the best way to understand shift operators is to take a look at this code :
a << b
public static long
leftShift (int a, int b, typeLength ) {
double c= a*((b<typeLength-1)? pow(2,b):
pow(2,b%typeLength));
return (long) c;
}
a >> b
public static long
rightShift ( int a, int b, int typeLength ){
double c=(b<typeLength-1)? pow(2,b) :
pow(2,b%typeLength));
double d=a/c;
double result = floor(d);
return (long)result;
}
consider the parameter typeLength as the length
of the result variable.
so for : int a = 2<<4; // typelength = 32
long b = 2<<4 // typelength = 64
tell me something if you don't understand
best regards
Luis Meira
 
Gautam Sewani
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Luis,
The only thing I can say about you is that you better not become a teacher
 
luis meira
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gautam Sewani:
Hi Luis,
The only thing I can say about you is that you better not become a teacher


god would't be that bad.
now seriously what you don't understand ?
 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi luis,
what you've posted are ways to calculate the result of shiftings, which require some pondering.
However, for understanding bitwise operations I'd go the bit way.
Gautam, maybe this one will help you, too:
http://www.javaranch.com/campfire/StoryBits.jsp
reply
    Bookmark Topic Watch Topic
  • New Topic