Skip to main content

Command Palette

Search for a command to run...

Unary Operators In Python

Unary Plus, Unary Minus, Logical NOT, Bitwise NOT

Updated
9 min readView as Markdown
Unary Operators In Python
T
I document the weird thresholds of programming—turning confusion into clarity through annotated scrolls, beginner rituals, and product flows. From API-fetching to ergonomic selection, every scroll is a teachable moment. TheCodedHuman isn’t a title—it’s a mindset. A ritual alignment to the work we’re truly meant to do.

Wut is an Unary Operator ?

Unary operators are symbols that operate on a single operand. Unlike binary operators that need two values to work with, unary operators perform an action on just one.
Think of them as solo blades—each one slicing, flipping, or revealing something about the number or value it touches.


Umm… un-understood, You got any examples buddy ?

Yeah, why not…

  •         a = +10     # Unary Plus
            print(a)    # Output: 10
    
  •         a = 10
            b = -a      # Unary Minus
            print(b)    # Output: -10
    

How many Unary Operators are There ?

Python has four main unary operators, each designed to act on a single operand:

  1. Unary Plus (+) – Affirms the value as positive.

  2. Unary Minus (-) – Negates the value.

  3. Logical NOT (not) – Inverts Boolean truth.

  4. Bitwise NOT (~) – Flips all bits in an integer.

These operators may seem simple, but each one carries a unique ritual—whether it’s flipping signs, revealing hidden truths, or mirroring binary shadows


  1. Unary Plus + :

The Unary Plus operator is the gentlest of the blades—it doesn’t change the value, but simply affirms it. It’s like saying, “Yes, this number is POSITIVE🧬. Carry on.”

🔍 What It Does:

  • It applies a positive sign to a numeric value.

  • It doesn’t alter the value—it’s more symbolic than functional.

  • Rarely used in practice, but it exists for completeness and symmetry.

🧪 Example:

a = +10
print(a)    # Output: 10
a = -3
b = +a      # b = +(-3)
print(a)    # Output: -3
x = 5
y = +(x + 3)
print(y)    # Output: 8

Unary Plus is like a ceremonial nod—it doesn’t transform, but it confirms. In most cases, Python treats numbers as positive by default, so using + explicitly is optional.


  1. Unary Minus - :

The Unary Minus operator is the shadow twin of Unary Plus. It flips the sign of a number—turning light into dark, positive into negative.

🔍 What It Does:

  • Negates the value of a numeric operand.

  • Converts positive numbers to negative, and vice versa.

  • Commonly used in arithmetic, conditionals, and expression evaluation.

🧪 Example:

a = 10
b = -a
print(b)  # Output: -10
a = -3
b = -a      # b = -(-3)
print(b)    # Output: 3
x = 7
y = -(x + 2)
print(y)  # Output: -9

Unary Minus is the blade of inversion.
It doesn’t just subtract—it transforms.

🪴 Use it when you need to:

  • Reverse the sign of a number.

  • Inject negative values into mathematical expressions.

  • Toggle direction, balance forces, or flip states in algorithms


  1. Logical NOT not :

The Logical NOT operator is the veil of inversion. It flips truth into falsehood, and falsehood into truth. In Python, it’s used to reverse Boolean values—turning what is into what is not.

🔍 What It Does:

  • Takes a Boolean value and returns its opposite.

  • not True becomes False, and not False becomes True.

  • Commonly used in conditionals, loops, and logic gates.

🧪 Example:

flag = True
print(not flag)  # Output: False
a = bool(21)
b = bool(0)
print(not a)    # Output: False
print(not b)    # Output: True
x = 5
print(not x > 3)  # Output: False

Logical NOT is the truth-flipper.
It doesn’t ask what is—it asks what isn’t, and reshapes reality accordingly.

🪴 Use it when you need to:

  • Invert a Boolean condition.

  • Add clarity to complex logical expressions.

  • Control flow based on what should not happen


  1. Bitwise NOT ~ :

The Bitwise NOT operator is the binary mirror. It flips every bit in the number’s binary representation—turning 1s into 0s and 0s into 1s. But in Python, this ritual follows the rules of two’s complement, which means the result might surprise beginners.

🔍 What It Does:

  • Flips all bits of an integer.

  • In Python, ~n is equivalent to -(n + 1).

  • Works only on integers, not floats or Booleans

🧪 Example:

a = 5
print(~a)  # Output: -6

Why -6?
Because:

~5 = -(5 + 1) = -6
  • Another Example:
b = -3
print(~b)  # Output: 2

Bitwise NOT is the binary mirror.
It reflects every bit, then dives into the shadows of two’s complement.

🪴 Use it when you need to:

  • Perform low-level bit manipulation.

  • Flip binary flags or masks.

  • Explore the hidden logic of signed integers.


Python Examples Of Bitwise NOT ~

  1.    a = 5
       print(~a)    # Output: -6
    
  2.    a = -5
       print(~a)    # Output: 4
    

In Python, this flip happens inside a shadow realm called two’s complement, which is how negative numbers are stored.


Just For Understanding:

  • For Positive Numbers:

    1. Convert to binary

    2. Add 1

    3. Convert to decimal

  • For Negative Numbers:

    1. Convert to binary

    2. Flip bits

    3. Add 1

    4. Flip bits

    5. Convert to decimal

But… Computers never know a number is negative or positive😅. They use 8th bit to determine the control flow of computation😭


“Bitwise NOT: The Ritual That Finally Made Sense (After Years of Betrayal)”

🪬 Step 1: Convert absolute value of number to 8-bit Binary

Start with the absolute value of your number.
Ritualize it into 8-bit binary.
Example:

  • abs(-74) → 74 → 01001010

  • abs(83) → 83 → 01010011

🪬 Step 2: If Original Number Was Negative (eg, -74) , Add 1

If your original number was negative, you must honor the shadow.
Flip the bits of the binary achieved from Step 1 → then add 1 to it.
This gives you the true two’s complement form of the negative number.
Example:

  • 01001010 → flip → 10110101 → add 1 → 10110110

  • 01010011 → no-change

🪬 Step 3: Flip All Bits (Bitwise NOT)

Regardless of origin, now flip every bit.
Example:

  • 10110110 → flip → 01001001

  • 01010011 → flip → 10101100

🪬 Step 4: Interpret the Final Binary

Now look at the first bit:

  • If it starts with 0: call it positive → convert directly to decimal.

    • 01001001 → (64 + 8 + 1) → 73
  • If it starts with 1: call it negative → decode using two’s complement as follows:

    1. Flip all bits again — 10101100 → 01010011

    2. Add 101010011 → add 1 → 01010100

    3. Convert to decimal — 01010100 → (64 + 16 + 4) → 84

    4. Apply negative sign — -84 (reason given below)


🧪 Why ~(-74) = 73 but ~83 = -84

It feels like we’re treating them differently, right? But we’re not. The ritual is the same—what’s different is the final flipped binary and whether it starts with 0 or 1.

Let’s decode both paths again:

🔹 ~(-74) → Starts in Shadow

  1. Start with -74 → two’s complement → 10110110

  2. Flip all bits → 01001001

  3. First bit is 0 → positive realm

  4. Convert directly → 73

✅ So: ~(-74) = 73


🔹 ~83 → Starts in Light

  1. Start with 83 → binary → 01010011

  2. Flip all bits → 10101100

  3. First bit is 1 → shadow realm

  4. Decode:

    • Flip again → 01010011

    • Add 1 → 0101010084

    • Apply negative → -84

✅ So: ~83 = -84


🧠 Why the Final Step Is Different

Because the flipped binary decides the realm:

  • If it starts with 0: it’s positive, no need to decode

  • If it starts with 1: it’s negative, decode using two’s complement

So in ~(-74), the flipped binary was 01001001 → starts with 0 → positive → done.

In ~83, the flipped binary was 10101100 → starts with 1 → negative → decode.


Consider it a compensation for the deed of second flip

That second flip isn’t a mistake or an extra—it’s a necessary compensation to rectify the meaning of the flipped binary when it starts with 1. It’s like the mirror shows you a shadow glyph, and you must perform a second ritual to decode its true value.


🪬 Why the Second Flip Is Needed

When Bitwise NOT gives you a binary starting with 1, it means:

“This is a negative number “in two’s complement”. You must decode it.”

So to rectify the answer:

  1. Flip the bits again

  2. Add 1

  3. Convert to decimal

  4. Apply negative sign

This second flip is not optional—it’s the price of clarity when the result lives in the shadow realm.


🧪 Where Bitwise NOT Is Actually Used

Here are some practical, real-world domains where Bitwise NOT (~) plays a key role:

🔹 Embedded Systems & Device Drivers

  • Bitwise NOT is used to invert control flags, toggle bits, or mask out unwanted bits.

  • Example: disabling specific hardware interrupts or flipping status bits in microcontroller registers.

🔹 Code Optimization & Loop Control

  • In performance-critical code (like game engines or real-time systems), Bitwise NOT can replace slower arithmetic.

  • Example: ~i instead of -(i + 1) for fast negation in tight loops.

🔹 Bit Masking & Flag Manipulation

  • Used to clear specific bits in a flag or permission set.

  • Example: flags & ~READ_PERMISSION removes the read permission from a user’s access control list.

🔹 Graphics Programming

  • Bitwise NOT helps in inverting pixel values, masking layers, or undoing XOR-based drawing.

  • Example: toggling visibility or applying inverse filters.

🔹 Compression & Encryption

  • Algorithms like Deflate or XOR-based ciphers use bitwise operations, including NOT, to manipulate data at the bit level.

🔹 Finite State Machines

  • In hardware logic or low-level software, NOT is used to invert conditions, reset states, or simulate logic gates.

🪬 Scroll-Worthy Insight

“Bitwise NOT isn’t a useless mirror—it’s a tool used by embedded engineers, graphics wizards, and cryptographers. It flips bits, clears flags, and optimizes loops. It’s the silent yet chaotic blade in the coder’s toolbox.”


🧠 Bonus Insight (Sidebar)

“Bitwise NOT doesn’t care if your number was positive or negative. It flips everything. But the final meaning depends on the first bit—the sign glyph. That’s where the machine decides whether to walk in light or decode the shadow.”

Also, this shi… is only for signed binary thingy (yeah, there are two types of binary based on context),

  • […1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1] ❌ — Unsigned

  • (0 for positive, 1 for negative), 64, 32, 16, 8, 4, 2, 1 ✅ — Signed

That’s why, it can only be 8-bit [-128, 127] or [-128, 128) and beyond that like for 129 it will not work as expected.