Bitwise Operations
Bitwise operators are tools that operate on individual bits of integers, allowing us to manipulate binary data at the lowest level. In this walkthrough, you’ll be guided through the process of crafting a C program that showcases the functionalities of bitwise AND, OR, XOR, and negation operations. These operations provide a powerful mechanism for fine-grained control over bits, which is particularly useful in scenarios where precision at the binary level is required.
Reminder of Bitwise Operations
1. Bitwise AND (&):
The bitwise AND operator (&) performs a binary AND operation between corresponding bits of two integers. If both bits are 1, the result is 1; otherwise, it’s 0.
Example:
2. Bitwise OR (|):
The bitwise OR operator (|) performs a binary OR operation between corresponding bits of two integers. If at least one of the bits is 1, the result is 1; otherwise, it’s 0.
Example:
Bitwise XOR (^):
The bitwise XOR operator (^) performs a binary XOR (exclusive OR) operation between corresponding bits of two integers. If the bits are different, the result is 1; if the bits are the same, the result is 0.
Bitwise NOT/Negation (~)
The bitwise NOT operator (~) performs a unary operation, inverting each bit of the operand. If the bit is 0, it becomes 1, and if the bit is 1, it becomes 0.
The result of the NOT operation is often interpreted in two’s complement form for signed integers.
About two’s compliment:
-
Two’s complement is a mathematical method of representing signed (positive, negative or zero) numbers in binary on computer systems. With two’s complement, when the most significant bit (the leftmost digit) in a binary number is 1 the number is signed as negative, and when it is 0 the number is signed as positive.
-
For example, two’s complement of 4 in binary (
0100) becomes1100to represent -4.
How to calculate two’s complement?
Calculating the two’s complement of a binary integer can be done in two ways:
- Method 1: Invert every digit in the binary number, then add 1.
- Method 2: Find the rightmost ‘1’ digit in the binary number, leave it as it is and invert every digit to the left of it.
Task 1
- Create a new folder in
Learning_C\and call itBitwiseOperations, then createa new file calledbitwiseoperations.c

-
Reproduce the following:
-
After the
#include <stdio.h>line addvoid bitwiseOperations(int a, int b); -
Go to the closing
}, line 5, ofmain()and after it reproduce the following code to so that the declaration of thebitwiseOperations()actually does something:void bitwiseOperations(int a, int b) { // AND operation int andResult = a & b; printf("AND result: %d & %d = %d\n", a, b, andResult); // OR operation int orResult = a | b; printf("OR result: %d | %d = %d\n", a, b, orResult); // XOR operation int xorResult = a ^ b; printf("XOR result: %d ^ %d = %d\n", a, b, xorResult); // Negation operation int negationA = ~a; int negationB = ~b; printf("Negation result: ~%d = %d, ~%d = %d\n", a, negationA, b, negationB); }-
Bitwise AND (
&):int andResult = a & b;performs a bitwise AND operation between corresponding bits of integersaandb.printf("AND result: %d & %d = %d\n", a, b, andResult);prints the result using the%dformat specifier in theprintfstatement.
-
Bitwise OR (
|):int orResult = a | b;performs a bitwise OR operation between corresponding bits of integersaandb.printf("OR result: %d | %d = %d\n", a, b, orResult);prints the result using the%dformat specifier in theprintfstatement.
-
Bitwise XOR (
^):int xorResult = a ^ b;performs a bitwise XOR operation between corresponding bits of integersaandb.printf("XOR result: %d ^ %d = %d\n", a, b, xorResult);prints the result using the%dformat specifier in theprintfstatement.
-
Bitwise Negation (
~):int negationA = ~a;andint negationB = ~b;perform bitwise negation operations on integersaandb, respectively.printf("Negation result: ~%d = %d, ~%d = %d\n", a, negationA, b, negationB);prints the negation results using the%dformat specifier in theprintfstatement.
These
printfstatements help visualize and understand the results of each bitwise operation by displaying the values of the operands and their outcomes. The%dformat specifier is used to print integers in the output. -
-
Modify
main()to look like this:int main() { // Example values for demonstration int value1 = 15; int value2 = 7; printf("Bitwise operations demonstration:\n"); bitwiseOperations(value1, value2); return 0; }The
mainfunction serves as the entry point of the program. It is the function that is automatically called when the program is executed. Let’s break down its components:-
Example Values:
int value1 = 15;andint value2 = 7;declare two integer variables (value1andvalue2) and assign them example values. These values are chosen for demonstration purposes and will be used as operands in the bitwise operations.
-
Function Call: the example values
value1andvalue2as arguments. This initiates the execution of the bitwise operations on these values. -
Print Statement:
printf("Bitwise operations demonstration:\n");prints a message to the console indicating that the program is demonstrating bitwise operations. This provides a clear context for the output that follows.
-
Return Statement:
return 0;signifies the successful completion of the program. In C, a return value of 0 conventionally indicates that the program executed without errors.
-
-
Run the program and predict the outputs:
Task 2
-
Modify the program to take user input for the values of
value1andvalue2instead of using predefined values. This will allow you to interactively input values and observe the bitwise operations in action. -
Add more bitwise operations to the
bitwiseOperationsfunction. For example, introduce left shift (<<) and right shift (>>) operations. Explain their purpose and demonstrate their usage. -
Modify the
printfstatements to display the results in hexadecimal format using%xformat specifier. This will introduce you to the hexadecimal representation of numbers. -
Experiment with bit manipulation techniques. For example, check if a specific bit is set or unset, and set or clear a particular bit.
-
If you have done it correctly you should get this output:
Full code
#include <stdio.h> int main(){ // Example values for demonstration int value1 = 0; int value2 = 0; printf("Type first value: \n"); scanf("%d", &value1); printf("Type second value: \n"); scanf("%d", &value2); printf("Bitwise operations demonstration:\n"); bitwiseOperations(value1, value2); return 0; } void bitwiseOperations(int a, int b) { // AND operation int andResult = a & b; printf("AND result: %d & %d = %d\n", a, b, andResult); // OR operation int orResult = a | b; printf("OR result: %d | %d = %d\n", a, b, orResult); // XOR operation int xorResult = a ^ b; printf("XOR result: %d ^ %d = %d\n", a, b, xorResult); // Negation operation int negationA = ~a; int negationB = ~b; printf("Negation result: ~%d = %d, ~%d = %d\n", a, negationA, b, negationB); // Left shift operation int leftShiftResult = a << 1; printf("Left Shift result: %d << 1 = %d\n", a, leftShiftResult); // Right shift operation int rightShiftResult = b >> 1; printf("Right Shift result: %d >> 1 = %d\n", b, rightShiftResult); // Check if the 3rd bit is set in a if ((a & (1 << 2)) != 0) { printf("3rd bit is set in %d\n", a); } else { printf("3rd bit is not set in %d\n", a); } // Set the 4th bit in b b = b | (1 << 3); printf("After setting 4th bit: %d\n", b); }

