A Summary on Bit Manipulation or Bitwise Operation.

A bit is the smallest unit of possible computation on a given data, usually all data even its KB, MB e.t.c forms are all fundamentally made up of bits, then bits are fundamentally made of 1’s and 0’s binary.

That’s all the computer kind of understands, thanks to John Von Neuman, thus in the language known as C and other languages, the possibility of pocking at these 1’s and 0’s become possible, in a word where abstraction is thrown away, and knowing how to convert from one base to another becomes crucial, where solving by paper becomes far more thrilling than waiting for the computer to spit out the answer, a world where you can manipulate the 1’s and 0’s, welcome to the word of Bitwise Operation and no you’re not Neo.

Bitwise Operation

Bitwise operations according to literature and articles I’ve read have said Bit Operation is a set of operations that can be used on binary numbers to alter their output or generate a new output.

Motivation for studying bitwise operators.

  • They are fun and cool.

  • we get to be Neo for a change and manipulate some binary numbers

  • The Prof said so

  • Bit operators are used for the optimization of embedded system

  • The exclusive or operator can be used to confirm the integrity of a file if it has been corrupted

  • Bitwise operations are used in compression

  • e.t.c

Bitwise Operators

There exist the following operators

  • & Bitwise AND

  • | Bitwise OR

  • ^ Bitwise XOR

  • ~ Bitwise NOT (Complement)

  • << Left Shift

  • \>> Right Shift

They might sound a lot like the Logical Operators AND &&, OR || e.t.c, but they aren’t these are way cooler, with the bitwise operators we can microscope on a single bit and tell it, what we want from it, get to affect it, we become the gatekeepers of the 1’s and 0’s, non shall pass except we have manipulated or spared. sorry!

Bitwise AND operator (&)

The AND operator will return TRUE (1) if and only if both operands are TRUE else it will return FALSE (0).

#include <stdio.h>


int bitwise_and(unsigned int x, unsigned int y){
    if (x <= 0)
        printf("%s", "FALSE");

    return x & y;

}


int main (void) {
    unsigned int returned_and = bitwise_and(10, 12);
    printf("%d", returned_and);
    return (0);
}

in more detail, I’d recommend brushing up on your converting decimal to binary, for the arguments 10 and 12 are converted to their binary form, once done the AND is applied. DO EXCUSE MY DRAWING SKILLS.

10 == 1 0 1 0

12 = 1 1 0 0

Applying the AND to both operands in their binary form output is 1 0 0 0 == 8 🤯🤯🤯🤯

NOTE: From here on out I’ll be using 1 == 1 0 1 0 and 12 == 1 1 0 0 to show the outputs of the other operators. Thank You.

Bitwise OR operator (|)

The bitwise OR will return TRUE (1) if there exists at least one operand with a 1 else it will return 0.

#include <stdio.h>

unsigned int bitwise_or(unsigned int x, unsigned int y)
{
    if (x <= 0)
        printf("%s", "FALSE");

    return (x | y);
}

int main (void) {
    printf("%d", bitwise_or(10, 12));
    return (0);
}

Bitwise XOR operator (^)

The XOR operator returns TRUE (1) if both operands are different but if they are the same it will return a False (0).

#include <stdio.h>



unsigned int bitwise_xor(unsigned int x, unsigned int y)
{
    if (x <= 0)
        printf("%s", "FALSE");

    return (x ^ y);
}

int main (void) {
    printf("%d", bitwise_xor(10, 12));
    return (0);
}

Bitwise NOT (~) operator

The Not is a unary operator, that is it operates with a single operand ~ operand. Hands down one of the coolest operators out there, like SpongeBob’s spatula, it flips bits to their opposite value, 1 becomes 0, 0 becomes 1, the day becomes night, night becomes day, Mrs. Puff becomes Trump, SpongeBob becomes Obama. Chaos!