The AND, OR, Exclusive OR, one complement and bitwise operators and their operations C program example
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Header file: Standard
Additional library: none/default
Additional project setting: Set project to be compiled as C
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)
Other info: none
To do: Doing the AND, OR, Exclusive OR, one complement and bitwise operators and their operations in C programming
To show: The AND, OR, Exclusive OR, one complement and bitwise operators and their operations C program example
// the bitwise operators and their operations example
#include <stdio.h>
// function prototype, you will learn later
void BitwiseOp(unsigned int);
int main(void)
{
unsigned int num1, num2, num3, mask, SetBit;
num1 = 7535;
mask = 1;
printf("The result of ANDing the following numbers\n");
printf("using the bitwise AND, & is\n");
// Display in normal and binary representation
BitwiseOp(num1);
BitwiseOp(mask);
printf(" ------------------------\n");
// function call, bitwise AND operation
BitwiseOp(num1 & mask);
num1 = 15;
SetBit = 241;
printf("\nThe result of inclusive ORing the following numbers\n");
printf("using the bitwise inclusive OR, | is\n");
BitwiseOp(num1);
BitwiseOp(SetBit);
printf(" ------------------------\n");
// function call, bitwise inclusive OR operation
BitwiseOp(num1 | SetBit);
num1 = 249;
num2 = 299;
printf("\nThe result of exclusive ORing the following numbers\n");
printf("using the bitwise exclusive OR, ^ is\n");
BitwiseOp(num1);
BitwiseOp(num2);
printf(" ------------------------\n");
// function call, bitwise exclusive OR operation
BitwiseOp(num1 ^ num2);
num3 = 21321;
printf("\nThe One's complement of\n");
BitwiseOp(num3);
printf(" |||||||| ||||||||\n");
// One's complement operation function call, bitwise negate operation
BitwiseOp(~num3);
return 0;
}
// function definition
void BitwiseOp(unsigned int value)
{
unsigned int p;
// Two 8 bits, 16 position, shift to left
unsigned int DisplayMask = 1 << 15;
printf("%7u = ", value);
// Loop for all bit...
for(p=1; p<=16; p++)
{
// if TRUE set to '1', otherwise set to '0'
putchar(value & DisplayMask ? '1':'0');
// shift to left bit by bit equal to: value << + 1 statement
value <<= 1;
if(p % 8 == 0)
// put a space
putchar(' ');
}
putchar('\n');
}
Output example:
The result of ANDing the following numbers
using the bitwise AND, & is
7535 = 00011101 01101111
1 = 00000000 00000001
------------------------
1 = 00000000 00000001
The result of inclusive ORing the following numbers
using the bitwise inclusive OR, | is
15 = 00000000 00001111
241 = 00000000 11110001
------------------------
255 = 00000000 11111111
The result of exclusive ORing the following numbers
using the bitwise exclusive OR, ^ is
249 = 00000000 11111001
299 = 00000001 00101011
------------------------
466 = 00000001 11010010
The One's complement of
21321 = 01010011 01001001
| | | | | | | | | | | | | | | |
4294945974 = 10101100 10110110
Press any key to continue . . .