Let's take it bitwise!
I am Jyotiprakash, a deeply driven computer systems engineer, software developer, teacher, and philosopher. With a decade of professional experience, I have contributed to various cutting-edge software products in network security, mobile apps, and healthcare software at renowned companies like Oracle, Yahoo, and Epic. My academic journey has taken me to prestigious institutions such as the University of Wisconsin-Madison and BITS Pilani in India, where I consistently ranked among the top of my class.
At my core, I am a computer enthusiast with a profound interest in understanding the intricacies of computer programming. My skills are not limited to application programming in Java; I have also delved deeply into computer hardware, learning about various architectures, low-level assembly programming, Linux kernel implementation, and writing device drivers. The contributions of Linus Torvalds, Ken Thompson, and Dennis Ritchie—who revolutionized the computer industry—inspire me. I believe that real contributions to computer science are made by mastering all levels of abstraction and understanding systems inside out.
In addition to my professional pursuits, I am passionate about teaching and sharing knowledge. I have spent two years as a teaching assistant at UW Madison, where I taught complex concepts in operating systems, computer graphics, and data structures to both graduate and undergraduate students. Currently, I am an assistant professor at KIIT, Bhubaneswar, where I continue to teach computer science to undergraduate and graduate students. I am also working on writing a few free books on systems programming, as I believe in freely sharing knowledge to empower others.
Here's a detailed code sample illustrating the use of bitwise operators in C with explanations and output:
#include <stdio.h>
void displayBinary(int num) {
// Display the binary representation of a number
for (int i = sizeof(int) * 8 - 1; i >= 0; i--) {
printf("%d", (num >> i) & 1);
if (i % 4 == 0) printf(" "); // Add space for better readability
}
printf("\n");
}
int main() {
// Bitwise AND (&)
int a = 14, b = 9;
int andResult = a & b;
printf("Bitwise AND: %d & %d = %d\n", a, b, andResult);
displayBinary(a);
displayBinary(b);
displayBinary(andResult);
// Bitwise OR (|)
int orResult = a | b;
printf("\nBitwise OR: %d | %d = %d\n", a, b, orResult);
displayBinary(a);
displayBinary(b);
displayBinary(orResult);
// Bitwise XOR (^)
int xorResult = a ^ b;
printf("\nBitwise XOR: %d ^ %d = %d\n", a, b, xorResult);
displayBinary(a);
displayBinary(b);
displayBinary(xorResult);
// Bitwise NOT (~)
int notResult = ~a;
printf("\nBitwise NOT: ~%d = %d\n", a, notResult);
displayBinary(a);
displayBinary(notResult);
// Left Shift (<<)
int leftShiftResult = a << 2;
printf("\nLeft Shift: %d << 2 = %d\n", a, leftShiftResult);
displayBinary(a);
displayBinary(leftShiftResult);
// Right Shift (>>)
int rightShiftResult = a >> 2;
printf("\nRight Shift: %d >> 2 = %d\n", a, rightShiftResult);
displayBinary(a);
displayBinary(rightShiftResult);
return 0;
}
Explanation:
Bitwise AND (
&):Performs a bitwise AND operation on each pair of corresponding bits.
Example:
14 & 9 = 8Binary representation:
14: 0000 0000 0000 0000 0000 0000 0000 1110 9: 0000 0000 0000 0000 0000 0000 0000 1001 & : 0000 0000 0000 0000 0000 0000 0000 1000
Bitwise OR (
|):Performs a bitwise OR operation on each pair of corresponding bits.
Example:
14 | 9 = 15Binary representation:
14: 0000 0000 0000 0000 0000 0000 0000 1110 9: 0000 0000 0000 0000 0000 0000 0000 1001 | : 0000 0000 0000 0000 0000 0000 0000 1111
Bitwise XOR (
^):Performs a bitwise XOR (exclusive OR) operation on each pair of corresponding bits.
Example:
14 ^ 9 = 7Binary representation:
14: 0000 0000 0000 0000 0000 0000 0000 1110 9: 0000 0000 0000 0000 0000 0000 0000 1001 ^ : 0000 0000 0000 0000 0000 0000 0000 0111
Bitwise NOT (
~):Inverts each bit in the operand.
Example:
~14 = -15Binary representation:
14: 0000 0000 0000 0000 0000 0000 0000 1110 ~ : 1111 1111 1111 1111 1111 1111 1111 0001
Left Shift (
<<):Shifts the bits of the left operand to the left by a specified number of positions.
Example:
14 << 2 = 56Binary representation:
14: 0000 0000 0000 0000 0000 0000 0000 1110 <<: 0000 0000 0000 0000 0000 0000 0011 1000
Right Shift (
>>):Shifts the bits of the left operand to the right by a specified number of positions.
Example:
14 >> 2 = 3Binary representation:
14: 0000 0000 0000 0000 0000 0000 0000 1110 >>: 0000 0000 0000 0000 0000 0000 0000 0011
Output:
Bitwise AND: 14 & 9 = 8
14: 0000 0000 0000 0000 0000 0000 0000 1110
9: 0000 0000 0000 0000 0000 0000 0000 1001
& : 0000 0000 0000 0000 0000 0000 0000 1000
Bitwise OR: 14 | 9 = 15
14: 0000 0000 0000 0000 0000 0000 0000 1110
9: 0000 0000 0000 0000 0000 0000 0000 1001
| : 0000 0000 0000 0000 0000 0000 0000 1111
Bitwise XOR: 14 ^ 9 = 7
14: 0000 0000 0000 0000 0000 0000 0000 1110
9: 0000 0000 0000 0000 0000 0000 0000 1001
^ : 0000 0000 0000 0000 0000 0000 0000 0111
Bitwise NOT: ~14 = -15
14: 0000 0000 0000 0000 0000 0000 0000 1110
~ : 1111 1111 1111 1111 1111 1111 1111 0001
Left Shift: 14 << 2 = 56
14: 0000 0000 0000 0000 0000 0000 0000 1110
<<: 0000 0000 0000 0000 0000 0000 0011 1000
Right Shift: 14 >> 2 = 3
14: 0000 0000 0000 0000 0000 0000 0000 1110
>>: 0000 0000 0000 0000 0000 0000 0000 0011