programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Shift operators

Ranch Hand
Posts: 129
Hi,

I am start learning the Shift operator in Campfire. It is excellent and very simple to learn. I am always struggle with shift operators, however this article helps me lot to understand the concept.

I have one doubt in this regard:

Ranch Hand
Posts: 657
11111111111111111111111111010110 (-42)
11111111111111111111111111101011 (-42 >> 1)
11111111111111111111111111110101 (-42 >> 2)
11111111111111111111111111111010 (-42 >> 3)
11111111111111111111111111111101 (-42 >> 4)
11111111111111111111111111111110 (-42 >> 5)

The bits on the right side fall off into the bit bucket as they're shifted off. The >> retains the value of the sign bit.

Hope this helps.
[ September 01, 2005: Message edited by: Steve Morrow ]

Java Cowboy
Sheriff
Posts: 16079
88
Negative numbers are stored in binary form as two's complement.

So -42 is: 11010110 (expressed as an 8-bit two's complement number)

When you shift the number right, the sign bit (the leftmost bit) will be extended, so -42 >> 5 gives you: 11111110, which is -2.

Rancher
Posts: 3742
16

Originally posted by Niyas Ahmed Sheikh:
128 64 32 16 8 4 2 1
0 0 0 1 1 0 0 0

20 = 00011000

I think a refresher course in decimal maths might help first.

[ September 01, 2005: Message edited by: Joanne Neal ]

 my overalls have superpowers - they repel people who think fashion is important. Tiny ad: The WEB SERVICES and JAX-RS Course https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS