• 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

Bit Shift operators

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all

Where can I find good understanding notes on bit shift operators. Please guide me.

Thanks
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
kalpana,
Kathy and Bert�s book (Sun Certified Programmer and Developer for Java 2 Study Guide (Exams 310-035 and 310-027))shows you exactly how it works.
But to summarize:

To determine the result of left shifting expression 16 >> 3 you do 16 divided by (2 to the power of 3) or 16/8 = 2. So 16 >> 3 = 2.

To determine the result of right shifting expression 8 << 4 you do 12 multiply by (2 to the power of 4) or 8*16 = 128. So 16 << 3 = 128.


I hope this will answer your question.
Christian

[ December 05, 2005: Message edited by: Christian Robitaille ]
[ December 05, 2005: Message edited by: Christian Robitaille ]
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a note: Everytime you shift right by 1 bit it's devided by 2. Everytime you shift left by 1 bit it's multiplied by 2.

Note: It's always integer division ie 3/2=1, 5/2=2, 6/2=3 etc....
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, it is noteworthy that there are two kinds of right shift operators. They differ in the way they treat negative numbers:
The signed right shift >> replicates the leftmost bit. In effect, this is the usual "divide-by-two-and-trunc" behaviour also for negative numbers.
The unsigned right shift >>> fills the left bits with zeroes, which yields a positive result. For a negative input, to my knowledge there is no easy rule of thumb what the result will be.

For char, there are no negative values, and the operators >> and >>> are equivalent.

By the way, what about

System.out.println(1<<-4);

I would have expected it to be either equivalent to
1 >> 4 or to 1 << (-4%32), but indeed it is equivalent to 1 << 29.
Is this legal, or is it a compiler issue (eclipse with jdk 1.5.0_04)?

Thanks in advance...

[ December 06, 2005: Message edited by: Tilo Hemp ]
[ December 06, 2005: Message edited by: Tilo Hemp ]
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Tilo, I ran a test script

public class bitshift{
public static void main(String[] args)
{
System.out.println(1<<-1);
System.out.println(1<<31);
System.out.println(1<<-2);
System.out.println(1<<30);
System.out.println(1<<-3);
System.out.println(1<<29);
System.out.println(1<<-4);
System.out.println(1<<28);
}

and here's the output:

-2147483648
-2147483648
1073741824
1073741824
536870912
536870912
268435456
268435456

Now can you tell us what you think is happening?
Sashi
 
Tilo Hemp
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sasikanth!

all I can say is

we found a question that possibly is even more abstruse than some we have to deal with during our preparation, and we even seem to have solved it
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic