• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

some operators are confusing

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello every body
this is my first day on javaranch.
i am hoping to attempt for scjp in april Ist week.
i am having problems in understanding
operators like
<<,>>,>>>e.t.c.
specially for negative numbers.
so plz any body can help me out.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi salaman,
Read through the Cat and Mouse Games with Bits article. It includes an applet that lets you play with different bit operators and shows the results.
I'm going to move this post to the Certification Study forum as it's not related to Results.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marcus Green's site is good and has a tutorial on bit shift operators: http://www.jchq.net/tutorial/BitShift.htm
Another good set of tips and explanation is at http://www.geocities.com/velmurugan_p/tips.html
a. shift operators allow a programmer to move the bit pattern of a number to the right or to the left.
b. in actual experience u may never use it but for the exam i think u need to understand it.
c. shifting can be to the left or the right. imagine it like moving red and white blocks sitting on a table. if u start to move the line to the left some of the blocks will fall of the table.
d. rule 1: blocks which fall of the table are lost. Bits that fall off during a shift are lost as well.
e. the reason bits "fall off" is because the the bits (which represent numbers byte, short, char, long etc) are of fixed length. so moving even one bit to the left or to the right will make the bit at the end of the line to fall off.
<<<>>>

f. in case there is a left shift (<<) or an unsigned right shift (>>>) the new bits are set to 0.
g. in case of the signed rt shif (>>) the new bits take the value of the most significant bit (MSB)
e.g 1000 0000 0000 0100 - represents an integer - negative because the MSB is 1
if we shift the above bit pattern 2 places to the right using the >>,
the 2 new bits on the left wil be 1s since the MSB is 1
resulting in the new bit pattern as:
1110 0000 0000 0000 0001
now all u need to know is how to do convert hese numbers in decimal.
----------------------------------------
shifting a bit to the left does double the value.
8421
0000 0001 - decimal 1
0000 0010 - decimal 2 .. this was shifted one bit to the left
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,Paratiti
If the shifting is grater than the bits the no is sifted by its modulus
e.g 234 >> 34 .
it is left shifted by 2 (34%32)
regards
vkswami
 
vkswamy venkatachalam
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,Paratiti
If the shifting is grater than the bits the no is sifted by its modulus
e.g 234 >> 34 .
it is left shifted by 2 (34%32)
regards
vkswami
 
Yeah. What he said. Totally. Wait. What? Sorry, I was looking at this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic