🏘️ JavaScript Bitwise NOT operator: Where Numbers Get New Outfits!
Once upon a time, in a place not far from your computer screen, there was a magical town called Bit Flip Town. In this town, every number had to wear a special jacket made up of 8 light switches. These tiny switches were called bits.
Each bit could either be:
- ON (represented by
1
) - OFF (represented by
0
)
These bits formed a number’s binary outfit — a code that told everyone exactly who that number was.
Now, here comes our hero — or should we say, our mischievous wizard — named Tilda. 🧙🏽♀️ She had one job: FLIP ALL THE BITS! That’s right. Every time someone passed by and shouted a number at her, she flipped their entire bit outfit — turning all 1
s into 0
s and all 0
s into 1
s.
Tilda’s favorite spell?
You guessed it… the Bitwise NOT operator: ~
🧮 Tilda’s Magic: The ~
Spell
Every time you do ~x
in JavaScript, here’s what happens:
💡 She turns
x
into-(x + 1)
It’s like taking the number, adding 1
to it, and making the answer negative!
Let’s visit a few villagers to see how Tilda changes their clothes…
🧙♀️ Tilda Flips the Town – One Bit at a Time
Here’s a table that shows what happens when the wizard Tilda casts her spell on each number:
Value (base 10) | Binary Outfit 👕 | Flipped Outfit 🧥 | Final Result 🎯 |
---|---|---|---|
2 | 00000010 |
11111101 |
-3 |
1 | 00000001 |
11111110 |
-2 |
0 | 00000000 |
11111111 |
-1 |
-1 | 11111111 |
00000000 |
0 |
-2 | 11111110 |
00000001 |
1 |
-3 | 11111100 |
00000010 |
2 |
🕵🏽 Let’s Sneak into Each Wardrobe
🧢 1. The Boy Named “2”
He wore a jacket like this: 00000010
Only one light was ON — the third from the right.
Tilda came along, said “Flip!”, and ZAP! — it became: 11111101
Now everyone called him -3 because:
~2 = -(2 + 1) = -3
🧢 2. The Little One — “1”
Original outfit: 00000001
Flip it and boom 💥 → 11111110
Which means:
~1 = -(1 + 1) = -2
🧢 3. The Baby “0”
Outfit: 00000000
— No lights ON!
After the flip: 11111111
— All lights turned ON!
Which is JavaScript’s way of saying:
~0 = -(0 + 1) = -1
🧢 4. Mister Grumpy “-1”
Outfit: 11111111
— All lights ON.
After Tilda flipped it: 00000000
— All OFF!
And guess what?
~(-1) = -(-1 + 1) = 0
🧢 5. Old Man “-2”
He came in with 11111110
Flip that → 00000001
Which means:
~(-2) = -(-2 + 1) = 1
🧢 6. Uncle “-3”
Wearing: 11111100
Tilda flipped him into: 00000010
And that’s 2
in disguise!
~(-3) = -(-3 + 1) = 2
🎩 The Secret Rule of Tilda’s Spell
Whenever you see ~x
, just remember this simple formula:
💫
~x = -(x + 1)
That’s how JavaScript plays this bit-flipping magic trick!
🎨 See It Again in Colorful Costumes
Here’s a recap for your comic-style notes:
🧍 Number | 👕 Binary Outfit | 🧥 Tilda’s Flip | 🎯 Final Answer |
---|---|---|---|
2 |
00000010 |
11111101 |
-3 |
1 |
00000001 |
11111110 |
-2 |
0 |
00000000 |
11111111 |
-1 |
-1 |
11111111 |
00000000 |
0 |
-2 |
11111110 |
00000001 |
1 |
-3 |
11111100 |
00000010 |
2 |
✍🏽 Time to Practice in Bit Flip Town!
Let’s see how much of a wizard YOU are now! 🧙🏽♂️
🎯 Practice Questions:
-
If you give Tilda the number
4
, what do you think she returns? (Hint: Use the rule~x = -(x + 1)
) -
What is the binary of
3
? What does it become when Tilda flips it? -
Why does
~0
always return-1
in JavaScript? -
Use the rule to find: What is
~(-5)
? -
If a binary number looks like
11111110
, what was the original number before the flip?
🎁 Bonus Challenge:
Write a JavaScript line that uses the bitwise NOT to check if a number is zero or not?
let x = 0;
if (~x) {
console.log("Not zero!");
} else {
console.log("It’s zero!");
}