I am trying to understand bit-wise shiting.
If I have 54 << 3 I know that 54 is represented in binary as 110110
How do I carry on to get to the final answer ?
Thanks for your help,
The example that you show shift the bit pattern three places to the left inserting zeroes on the new positions, so 54 << 3, will become 110110000 binary which equals decimal 432.
In general, each bit-shift to the left multiplies by 2, so here 54 is multiplied by 2*2*2=2^3, and, generally,
n << m = n*2^m
But beware of the number of available bits (normally 32).
Shifting to the right removes bits from the right, so 54 >> 3 would become 110 binary which is decimal 6. - Each bit-shift to the right divides by 2 and rounds (towards 0) to the closest integer.
Also, some compilers do funny things with the sign bit. Visual studio shifts everything as a unsigned (all the bits shift) but I have seen compilers shift an integer and leave the sign bit alone, for example
10001 << 1
10010 (some compilers)
I recently wrote an article about bitwise operators. It will shed some light on this topic:
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL