aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How this output is coming regarding left shift operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How this output is coming regarding left shift operator" Watch "How this output is coming regarding left shift operator" New topic
Author

How this output is coming regarding left shift operator

nagaraj raja
Ranch Hand

Joined: Aug 06, 2005
Posts: 36
Hai,
This is nagaraju i am confusing how this output is coming plz explain this code
how.plz explain
Sherry Jacob
Ranch Hand

Joined: Jun 29, 2005
Posts: 128
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 !!


<strong><br />Cheers !!<br /> <br />Sherry<br /></strong><br />[SCJP 1.4]
Santana Iyer
Ranch Hand

Joined: Jun 13, 2005
Posts: 335
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

Joined: Jun 13, 2005
Posts: 335
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

Joined: Aug 06, 2005
Posts: 36
Thanku....very much now i get clear idea about shift operator
Adil El mouden
Ranch Hand

Joined: Jun 01, 2005
Posts: 82
[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 ]

SCWCD 1.4(Loading...), SCJP 1.4(98%), Bachelor of Engineering (computer science)
sravan mandala
Greenhorn

Joined: Mar 13, 2001
Posts: 7
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
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

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 ]
 
wood burning stoves
 
subject: How this output is coming regarding left shift operator