Menu
Coddy logo textTech

Rotate bits of a number I

Lesson 12 of 17 in Coddy's Bit Manipulation course.

What does rotating the bits means?

Let's understand with an illustration.

In the above illustration we have taken (20)10 which is (10100)2 in binary and have rotated it rightwards once and we got (01010)2 which is (10)10. What if we rotate it leftwards? We will get (40)10

How come 40?

Well, in our computer systems the numbers are stored in a space of 32 bits or 16 bits or 8 bits. It is similar to the rotation that you saw in the above illustration but the number (20)10 in a 16 bit computer will be placed in memory as (000000000010100)2 ,total of 16 bits to save the the number 20 in memory, so when you will rotate this binary number you will have to take in account all the 16 bits rather than 5 bits only. Therefore, when you will try to rotate a number you may get different answers as answers will be based on the computer you are using i.e. either 32 bit or 16 bit or 8 bit.

Let's understand, how bit manipulation will help us in rotating bits in the next lesson.

 

Try it yourself

This lesson doesn't include a code challenge.

All lessons in Bit Manipulation