L
LLLOS.ai
Learn
L

Ask Your Tutor · Computer Science

Can you use bitwise AND with negative numbers?

From NCERT Asked by Anonymous from Marseille, France 23 Oct 2026, 2:52 PM IST Updated

In one line

Yes, you can use bitwise AND with negative numbers, but the result may look strange because of how computers store negatives.

The rule

Bitwise AND compares each bit of two numbers and gives 1 if both bits are 1, otherwise 0.

For two numbers a and b, the operation is written as a & b.

The formula
a & b
afirst number (can be negative)
&bitwise AND operation
bsecond number (can be negative)

Let's understand it simply

Imagine you have two rows of switches (like tube light switches in your home). Each switch can be ON (1) or OFF (0). Bitwise AND checks each pair of switches. If both are ON, the result is ON. If any one is OFF, the result is OFF.

But computers store negative numbers in a special way called two's complement. This means the bits for negative numbers look different than for positive numbers. So, when you do bitwise AND with a negative number, you are working with its special bit pattern.

How it works

  1. Write both numbers in binary (their bit patterns).
  2. For negative numbers, use two's complement form.
  3. Line up the bits of both numbers.
  4. For each bit position, write 1 if both bits are 1, else write 0.
  5. The result is a new binary number, which may also be negative.

The story behind it

Bitwise operations were made for computers to work directly with bits (the smallest unit of data: 0 or 1). Early computers needed fast ways to do simple checks, like turning ON/OFF parts of a machine. Two's complement was invented to make adding and subtracting negative numbers easy for computers.

Where you see it in real life

  • Checking permissions in apps: Apps use bitwise AND to check if you have permission (like read/write) by checking bits.
  • Masking numbers: To keep only certain digits (like last 4 digits of a phone number), bitwise AND is used.
  • Turning off/on devices: In microcontrollers (used in washing machines or fans), bitwise AND helps control which parts are ON.
  • Finding even/odd numbers: number & 1 tells if a number is even (0) or odd (1), even for negatives.

Why it matters

  • Used in programming, especially in C, C++, Java, and Python.
  • Needed for low-level work (hardware, electronics, robotics).
  • Makes some calculations much faster.
  • Important for coding interviews and computer exams.

Worked example

Let's try: What is -5 & 3?

Given: -5 and 3
Step 1: Write 3 in binary         0000 0011
Step 2: Write -5 in binary (two's complement for 8 bits)
        +5 is 0000 0101
        Invert bits: 1111 1010
        Add 1:      1111 1011 (this is -5)
Step 3: AND each bit:
        1111 1011
      & 0000 0011
      = 0000 0011
Step 4: 0000 0011 is 3 in decimal
Answer: -5 & 3 = 3

Common mistakes

  • Thinking bitwise AND works only for positive numbers. (It works for all integers!)
  • Forgetting that negative numbers have a different bit pattern (two's complement).
  • Mixing up bitwise AND (&) with logical AND (&& in C, and in Python).

Remember it

Bitwise AND works on all numbers, positive or negative—just remember, negatives have special bit patterns!

Check yourself

What is the result of -5 & 3?

3, because the bitwise AND of their bit patterns gives 3.

Why do negative numbers look different in binary?

Because computers use two's complement to store negatives.

Can you use bitwise AND to check if a number is even or odd, even if it's negative?

Yes, because the last bit (LSB) tells if it's even (0) or odd (1).

What is the difference between bitwise AND (&) and logical AND (&&)?

Bitwise AND works on bits; logical AND works on true/false values.

Quick recap

  • Bitwise AND works with both positive and negative numbers.
  • Negative numbers use two's complement form in computers.
  • The result of bitwise AND depends on the bit patterns.
  • It is used in programming, electronics, and security checks.
  • Don't confuse bitwise AND (&) with logical AND (&& or 'and').
Written by the LLOS.ai tutor, checked against NCERT chapters. Asked by Anonymous from Marseille, France on 23 Oct 2026, 2:52 PM IST
Asked by Visitor (Location unknown)