Bit Shifting: Moving Bits Like We Move Things Around!

Imagine you have a bunch of LEGO bricks all lined up. Bit shifting is kind of like sliding those bricks to the left or right. But instead of LEGOs, we’re talking about the tiny on/off switches inside your computer, which we call “bits.” These bits make up the numbers your computer understands.

🧠 What’s Bit Shifting All About?

When we “bit shift,” we’re taking all those 1s and 0s (the bits) in a number and pushing them to one side or the other. This isn’t just a simple shuffle — it actually changes the number itself, usually making it bigger or smaller in a predictable way. Think of it like moving the decimal point in regular numbers, but with powers of 2!

➡️ Left Shift (<<): Shoving Bits to the Left

📌 The Idea:

A “left shift” is like telling all the bits to move a certain number of steps to the left. Every time they take a step left, we put a brand new ‘0’ on the right end.

Guess what happens when you shift left? It’s the same as multiplying your number by 2 for each step you take!

🔍 Let’s See It:

Imagine the number 5. In computer language (binary), it looks like 00000101.

If we shift it to the left by just one step (5 << 1), it becomes:

00001010

Now, if you read this new binary number, it’s 10 in our regular counting!

So, 5 << 1 is the same as saying 5 multiplied by 2, which equals 10. Cool, right?

🔁 More Examples:

  • 3 << 2 is like 3 multiplied by 2 twice (which is 3×4), so it equals 12.

  • 7 << 3 is like 7 multiplied by 2 three times (which is 7×8), so it equals 56.

⬅️ Right Shift (>>): Pushing Bits to the Right

📌 The Idea:

A “right shift” is the opposite. We’re telling all the bits to move a certain number of steps to the right. When they move right, the bits on the far right end just disappear.

This time, shifting right is like dividing your number by 2 for each step you take, and we only keep the whole number part (no fractions!).

🔍 Let’s Check It Out:

Take the number 20. In binary, it’s 00010100.

If we shift it to the right by two steps (20 >> 2), it turns into:

00000101

And that binary number is 5 in our normal counting!

So, 20 >> 2 is like saying 20 divided by 4 (2 times 2), which equals 5.

🔁 More Examples:

  • 16 >> 1 is like 16 divided by 2, which equals 8.

  • 8 >> 3 is like 8 divided by 2 three times (which is 8÷8), so it equals 1.

Quick Look Table:

+----------------------------------------------------------------------------+
| What We Do | What Happens                                  | Example       |
|------------|-----------------------------------------------|---------------|
| x << n     | Multiply by 2 to the power of n               | 5 << 2 = 20   |
| x >> n     | Integer divide by 2 to the power of n         | 20 >> 2 = 5   |

💡 Why Bother with Bit Shifting?

  • Super Speedy: Your computer can do these bit moves way faster than regular multiplication or division. It’s like taking a shortcut!

  • Clever Tricks: Programmers use bit shifting for smart shortcuts in things like searching through data quickly, figuring out combinations, or using binary “on/off” switches (like light switches).

  • Talking to Machines: When computers need to talk directly to hardware (like parts inside your computer or network stuff) or understand special file formats, bit shifting is often the language they use.

0
Subscribe to my newsletter

Read articles from Shreehari Vaasistha L directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Shreehari Vaasistha L
Shreehari Vaasistha L