• 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
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

How this output is coming regarding left shift operator

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai,
This is nagaraju i am confusing how this output is coming plz explain this code
how.plz explain
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by nagaraj raja:
Hai,
This is nagaraju i am confusing how this output is coming plz explain this code
how.plz explain




Hi Nagaraj,
The answer is absolutely correct. Here's how:
We have b = 5.

Now, b << 33 means b is to be shifted left by 33 bits with zero fill on the right.

If we convert 5 into the binary form, we get :
0000 0000 0000 0000 0000 0000 0000 0101.

Shifting left one bit at a time gives ...
1) 0000 0000 0000 0000 0000 0000 0000 1010
2) 0000 0000 0000 0000 0000 0000 0001 0100
3) 0000 0000 0000 0000 0000 0000 0010 1000
4) 0000 0000 0000 0000 0000 0000 0101 0000
5) 0000 0000 0000 0000 0000 0000 1010 0000
6) 0000 0000 0000 0000 0000 0001 0100 0000
7) 0000 0000 0000 0000 0000 0010 1000 0000
8) 0000 0000 0000 0000 0000 0101 0000 0000
9) 0000 0000 0000 0000 0000 1010 0000 0000
10) 0000 0000 0000 0000 0001 0100 0000 0000
11) 0000 0000 0000 0000 0010 1000 0000 0000
12) 0000 0000 0000 0000 0101 0000 0000 0000
13) 0000 0000 0000 0000 1010 0000 0000 0000
14) 0000 0000 0000 0001 0100 0000 0000 0000
15) 0000 0000 0000 0010 1000 0000 0000 0000
16) 0000 0000 0000 0101 0000 0000 0000 0000
17) 0000 0000 0000 1010 0000 0000 0000 0000
18) 0000 0000 0001 0100 0000 0000 0000 0000
19) 0000 0000 0010 1000 0000 0000 0000 0000
20) 0000 0000 0101 0000 0000 0000 0000 0000
21) 0000 0000 1010 0000 0000 0000 0000 0000
22) 0000 0001 0100 0000 0000 0000 0000 0000
23) 0000 0010 1000 0000 0000 0000 0000 0000
24) 0000 0101 0000 0000 0000 0000 0000 0000
25) 0000 1010 0000 0000 0000 0000 0000 0000
26) 0001 0100 0000 0000 0000 0000 0000 0000
27) 0010 1000 0000 0000 0000 0000 0000 0000
28) 0101 0000 0000 0000 0000 0000 0000 0000
29) 1010 0000 0000 0000 0000 0000 0000 0000

After this step, the 1 in the most significant bit place, is shifted back to the least significant bit position by left shift

30) 0100 0000 0000 0000 0000 0000 0000 0001
31) 1000 0000 0000 0000 0000 0000 0000 0010
32) 0000 0000 0000 0000 0000 0000 0000 0101
33) 0000 0000 0000 0000 0000 0000 0000 1010

If we convert the last binary number into decimal, we get 10 which is the required answer.

Hope this solves your confusion !!
 
Ranch Hand
Posts: 335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int b=5;

b<<33

shift distance is calculated by Anding right hand operator % 32 (*)

so 33 % 32 which is 1

so b << 33 is equivalent to b << 1

(*) left hand operand b type is int so right hand % 32
if left hand operand is type is long than right hand % 64

other way

right hand operand Anding with 0x1f (31) if left hand operand is int.
right hand operand Anding with 0x3f (63) if left hand operand is long.

System.out.println( 5 << 33 == 5 << 1); // true

check it, correct me if anything is amiss.
hope this helps.
 
Santana Iyer
Ranch Hand
Posts: 335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry i made mistake in my third stmt,

it is like right hand operand (not operator) % 32

so 33 % 32 =1
so 5<<1 which is 10

sorry sorry
 
nagaraj raja
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanku....very much now i get clear idea about shift operator
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[B] how.plz explain[/B]



Another solution (easy one!):

33%32 =1
5 << 1 = 5*2 to power of 1

A left shift operator is actually causing 5 being shifted to be multiplied by 2 to the power of the number of bits to shift (here :1)

For the right operator:

33%32 = 1
5 >> 1 = 5 / 2 to power of 1

]A right shift operator is actually causing 5 being shifted to be divided by 2 to the power of the number of bits to shift (here :1)

Thanks K&B

Cheers
[ August 10, 2005: Message edited by: Adil El mouden ]
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Adil,

Thank you so much for your explination, but I am still confused about the shift operators.

How do we calculate if it is (b<<31) and (b >>31) I would really appreciate if you can explain this to me.

Thanks
Sham
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

How do we calculate if it is (b<<31) and (b >>31) I would really appreciate if you can explain this to me.

If we're talking about int values, only the five lowest-order bits are used. It's as if the number you're shifting by is &'ed with 31 (0x1F). In this case, 31 & 31 == 31. So, you'll shift left 31 times, with bits falling off into the void on the left hand side as necessary(e.g., 5 << 31 == -2147483648).

31 & 0x1F:
00000000 00000000 00000000 00011111
-----------------------------------

5:
00000000 00000000 00000000 00000101

5 << 1:
00000000 00000000 00000000 00001010

5 << 2:
00000000 00000000 00000000 00010100

...

5 << 29:
10100000 00000000 00000000 00000000

5 << 30:
01000000 00000000 00000000 00000000

5 << 31:
10000000 00000000 00000000 00000000


�15.19 JLS
[ August 15, 2005: Message edited by: Steve Morrow ]
 
Oh. Hi guys! Look at this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic