Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!

# Bit-shift operators and primitive types

Natalie Ap
Ranch Hand
Posts: 49

1) When i tried to find the value of long_int_var above after line 1, the value of the variable was 1!! How is that possible!
The value of the variable orig_long_var is 2. After shifting 3 times, shouldn't the value of the resultant long_int_var be 0!!

2) The vaue of long_long_var was 0, which was i understood why.

3) But again, the value of int_int_var was 1!! After shifting one would expect thevalue to be 0!! How come it is 1!

Thanks!

Balaji Bang
Ranch Hand
Posts: 182
Bitshift operators are not in the exam 1.5

Punit Singh
Ranch Hand
Posts: 952
this is out of scjp 5 or 6.0, are you preparing for scjp 1.4.

By the way:

int size is: 32 bits
orig_int_var >> 33; =orig_int_var >> (33-32); = 2>>1 =0000 0010>>1 =0000 0001 = 1.

long size is : 64 bits
orig_long_var >> 33; =(56 0s) 0000 0001 >> 33 = 64 0s = 0.

Prav sharma
Ranch Hand
Posts: 102
Punit Singh wrote:this is out of scjp 5 or 6.0, are you preparing for scjp 1.4.

By the way:

int size is: 32 bits
orig_int_var >> 33; =orig_int_var >> (33-32); = 2>>1 =0000 0010>>1 =0000 0001 = 1.

long size is : 64 bits
orig_long_var >> 33; =(56 0s) 0000 0001 >> 33 = 64 0s = 0.

Yes. This topic is not in scjp 5.
Anyway thanks for the explanation punit.

Cheers

Natalie Ap
Ranch Hand
Posts: 49
Prav sharma wrote:
Punit Singh wrote:this is out of scjp 5 or 6.0, are you preparing for scjp 1.4.

By the way:

int size is: 32 bits
orig_int_var >> 33; =orig_int_var >> (33-32); = 2>>1 =0000 0010>>1 =0000 0001 = 1.

long size is : 64 bits
orig_long_var >> 33; =(56 0s) 0000 0001 >> 33 = 64 0s = 0.

Yes. This topic is not in scjp 5.
Anyway thanks for the explanation punit.

Cheers

Hi All,

Thanks a lot for the replies. Thanks for the explanation Punit..

I am preparing for SCJP 1.5. But some of the mock exams online are out-dated and they have these questions. So I keep getting confused. Also, i get curious when i come across these questions.

Anyway, i know this must have been discussed here a lot of times, but can someone please give me a descriptive list of all those topics that are expected in 1.5? The objectives on the Sun site are not very much in detail and I keep presuming that certain things are bound to be included (something like bit-wise which is supposed to be basic)

Thanks a lot!!

Prav sharma
Ranch Hand
Posts: 102
Request you to prepare from Kathy Siera book for SCJP 5. It will cover all the required topics with Questions and Answers.

All the best

N Pats wrote:
Prav sharma wrote:
Punit Singh wrote:this is out of scjp 5 or 6.0, are you preparing for scjp 1.4.

By the way:

int size is: 32 bits
orig_int_var >> 33; =orig_int_var >> (33-32); = 2>>1 =0000 0010>>1 =0000 0001 = 1.

long size is : 64 bits
orig_long_var >> 33; =(56 0s) 0000 0001 >> 33 = 64 0s = 0.

Yes. This topic is not in scjp 5.
Anyway thanks for the explanation punit.

Cheers

Hi All,

Thanks a lot for the replies. Thanks for the explanation Punit..

I am preparing for SCJP 1.5. But some of the mock exams online are out-dated and they have these questions. So I keep getting confused. Also, i get curious when i come across these questions.

Anyway, i know this must have been discussed here a lot of times, but can someone please give me a descriptive list of all those topics that are expected in 1.5? The objectives on the Sun site are not very much in detail and I keep presuming that certain things are bound to be included (something like bit-wise which is supposed to be basic)

Thanks a lot!!

Ashish Hareet
Ranch Hand
Posts: 375
N Pats,

long long_int_var = orig_int_var >> 33;

orig_init_var is of type int & it's value is 1, so it's binary representation is as follows

00000000 00000000 00000000 00000010

Shift this right by 33 you end up with zero. Do note that the resut of the shift expression is a int cause orig_int_var is an int, no long was involved in the shift operation.

Explanation for your third question is same as above.

Try orig_int_var >> 2 for kicks.

Try binary shift operations - Binary Shift Applet
Article - Binary Shift Operations

HTH
Ashish Hareet

Punit Singh
Ranch Hand
Posts: 952
Ashish Hareet wrote:

orig_init_var is of type int & it's value is 2, so it's binary representation is as follows

00000000 00000000 00000000 00000010

Shift this right by 33 you end up with zero.

Shift this right by 33 means shift by (33-32) means by 1 so you end up with 1.

Ashish Hareet
Ranch Hand
Posts: 375
Thanks Punit