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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Paul Clapham
• Ron McLeod
• Bear Bibeault
• Liutauras Vilda
Sheriffs:
• Jeanne Boyarsky
• Tim Cooke
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Stephan van Hulst
• Jj Roberts
• Carey Brown
Bartenders:
• salvin francis
• Frits Walraven
• Piet Souris

# Difficulty of shift operator and hex conversion

Greenhorn
Posts: 12
• Number of slices to send:
Optional 'thank-you' note:
Hi All
I felt that there is a big difference between the difficulty of
bit shift operator questions in K&B book and in Dan's exams.
Can anyone help me as to which is more closer to the real exam.
As far as my aim goes, I would not like to get more headache from
the bit shifting than is absolutely necessary for the exam.
An example from K&B is (8>>2)<<4 or 128>>>2.
This I can very much handle but Dan's exams has things like
int i1 = 0xffffffff, i2 = i1 << 33;
int i3 = i1 << (33 & 0x1f);
System.out.print(Integer.toHexString(i2) + ",");
System.out.print(Integer.toHexString(i3));
I am not sure, if I need to spend time to solve this hard questions,
as to convert from hex to binary for such big numbers then do the bit
shifting and then convert the answer in hex again. I understand
the answers given by Dan and I think his site is of extra ordinary
caliber and I want to thank him for the site. From my experience in the
real field though, I don't see the use of bit shifting, all the rest
is nice to learn whether it is on the exam or not.
Thanks

Ranch Hand
Posts: 1865
• Number of slices to send:
Optional 'thank-you' note:
Suneela,
The conversions associated with that questions are actually very simple. Please see the this tutorial.
The explanation for that question needs some improvement. The next version of the exam will contain the following explanation.

For each of the three shift operators, <<, >> and >>>, the shift distance is specified by the right hand operand. If the left operand is of type int, then the shift distance is always within the range 0 to 31, inclusive; and the following expression is always true: (int1 << shift) == (int1 << (shift & 0x1f)). The hexadecimal representation of decimal 31 is 0x1f and the binary representation is 0001 1111. The hexadecimal representation of decimal 33 is 0x21 and the binary representation is 0010 0001. The expression i1 << (33 & 0x1f) is equivalent to (0xffffffff << (0x21 & 0x1f)). Evaluation of the right hand operand of the shift operator produces (0xffffffff << 1). The final result is 0xfffffffe. Similarly, if the left operand is of type long, then the shift distance is always within the range 0 to 63, inclusive; and the following expression is always true: (long1 << shift) == (long1 << (shift & 0x3f)).

I hope that helps.

Suneela Joshi
Greenhorn
Posts: 12
• Number of slices to send:
Optional 'thank-you' note:
Thanks, Dan, I will go through the tutorial. I guess, I don't feel
motivated to spend time on the bit shift. In several years of working
in oo programming, mainly in C++ and some in Java, I have never
had the need to do this bit shifting stuff and feel it is quite
useless in real world. I remember doing all this in my first C class
long time ago but since then it has just been a blurr memory. But
if you think it is necessary to pass the exam, I guess, that should
be the motivation to relearn it. Thank you again for the reply as well
as the wonderful site. Your exam are excellent.

Ranch Hand
Posts: 45
• Number of slices to send:
Optional 'thank-you' note:
Suneela,
If you are not a person targeting 100% score on the exam, and don't mind loosing 1 or 2(max) possible correct answers, you can *totally ignore* the shift-operators for the exam. Well, that is what I did. I find it hard to get the shift-operators on my head. So I just decided not to study it at all. And I had only one question on shift-operators in my scjp exam...and answered it on guess. I just did not want to spend all the time learning it...(and not use in real life programming). I rather spend time on learning threads...gc..exception..OO etc.
Now, that is just my humble opinion (many other people I know told me the same about the number of question on shift-operators), other may differ..
Good Luck

 Would you like to try a free sample? Today we are featuring tiny ads: the value of filler advertising in 2020 https://coderanch.com/t/730886/filler-advertising