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.
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
}
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 integersa
andb
.printf("AND result: %d & %d = %d\n", a, b, andResult);
prints the result using the%d
format specifier in theprintf
statement.
-
Bitwise OR (
|
):int orResult = a | b;
performs a bitwise OR operation between corresponding bits of integersa
andb
.printf("OR result: %d | %d = %d\n", a, b, orResult);
prints the result using the%d
format specifier in theprintf
statement.
-
Bitwise XOR (
^
):int xorResult = a ^ b;
performs a bitwise XOR operation between corresponding bits of integersa
andb
.printf("XOR result: %d ^ %d = %d\n", a, b, xorResult);
prints the result using the%d
format specifier in theprintf
statement.
-
Bitwise Negation (
~
):int negationA = ~a;
andint negationB = ~b;
perform bitwise negation operations on integersa
andb
, respectively.printf("Negation result: ~%d = %d, ~%d = %d\n", a, negationA, b, negationB);
prints the negation results using the%d
format specifier in theprintf
statement.
These
printf
statements help visualize and understand the results of each bitwise operation by displaying the values of the operands and their outcomes. The%d
format 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
main
function 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 (value1
andvalue2
) 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
value1
andvalue2
as 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
value1
andvalue2
instead 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
bitwiseOperations
function. For example, introduce left shift (<<
) and right shift (>>
) operations. Explain their purpose and demonstrate their usage. -
Modify the
printf
statements to display the results in hexadecimal format using%x
format 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.