C provides operators that let you perform logical operations on the individual bits of integer values, and shift the bits right or left.
The bitwise operators are:
& (bitwise AND) : Bitwise AND is a binary operator and requires two operands. When we use the bitwise AND operator, the bit in the first operand is ANDed with the corresponding bit in the second operand. The result of AND is 1 only when the bits in both operands are 1, otherwise it is 0.
| (bitwise OR) : Bitwise OR is a binary operator and requires two operands. When we use the bitwise OR operator, the bit in the first operand is ORed with the corresponding bit in the second operand. The result of OR is 0 only when the bits in both operands are 0, otherwise it is 1.
^ (bitwise XOR) : Bitwise XOR is a binary operator and requires two operands. When we use the bitwise XOR operator, the bit in the first operand is XORed with the corresponding bit in the second operand. The result of XOR is 1, if bits of both operands have different value, otherwise it is 0.
<< (bitwise left shift) : Bitwise left shift operator is used for shifting the bits left. It requires two operands; the left operand is the operand whose bits are shifted and the right operand indicates the number of bits to be shifted.
>> (bitwise right shift) : Bitwise right shift operator is used for shifting the bits right. It requires two operands; the left operand is the operand whose bits are shifted and the right operand indicates the number of bits to be shifted.
~ (bitwise NOT) : Bitwise NOT (One's complement) operator is an unary operator (works on only one operand). It changes 1 to 0 and 0 to 1.
The bitwise operators are:
Bitwise Operators |
& (bitwise AND) : Bitwise AND is a binary operator and requires two operands. When we use the bitwise AND operator, the bit in the first operand is ANDed with the corresponding bit in the second operand. The result of AND is 1 only when the bits in both operands are 1, otherwise it is 0.
| (bitwise OR) : Bitwise OR is a binary operator and requires two operands. When we use the bitwise OR operator, the bit in the first operand is ORed with the corresponding bit in the second operand. The result of OR is 0 only when the bits in both operands are 0, otherwise it is 1.
^ (bitwise XOR) : Bitwise XOR is a binary operator and requires two operands. When we use the bitwise XOR operator, the bit in the first operand is XORed with the corresponding bit in the second operand. The result of XOR is 1, if bits of both operands have different value, otherwise it is 0.
<< (bitwise left shift) : Bitwise left shift operator is used for shifting the bits left. It requires two operands; the left operand is the operand whose bits are shifted and the right operand indicates the number of bits to be shifted.
>> (bitwise right shift) : Bitwise right shift operator is used for shifting the bits right. It requires two operands; the left operand is the operand whose bits are shifted and the right operand indicates the number of bits to be shifted.
~ (bitwise NOT) : Bitwise NOT (One's complement) operator is an unary operator (works on only one operand). It changes 1 to 0 and 0 to 1.
The following program demonstrates bitwise operators:
#include <stdio.h>
int main(void)
{
unsigned int a = 4, b = 5; /* a = 4(00000100), b = 5(00000101) */
printf("a = %d, b = %d\n", a, b);
printf("a&b = %d\n", a&b); /* The result is 00000100 */
printf("a|b = %d\n", a|b); /* The result is 00000101 */
printf("a^b = %d\n", a^b); /* The result is 00000001 */
printf("~a = %d\n", a = ~a); /* The result is 11111011 */
printf("b<<1 = %d\n", b<<1); /* The result is 00001010 */
printf("b>>1 = %d\n", b>>1); /* The result is 00000010 */
return 0;
}
Output:
a = 4, b = 5
a&b = 4
a|b = 5
a^b = 1
~a = -5
b<<1 = 10
b>>1 = 2
The bitwise operators are discussed in detail in upcoming tutorials.
sizeof() Operator
sizeof is an unary operator that gives the size of its operand in terms of bytes. The operand can be a variable, constant or any datatype( int, float, char etc). For example,
sizeof(char) gives the size occupied by a char data type.
The following program demonstrates how you use sizeof() operator to check the size of fundamental data types on your system.
#include <stdio.h>
int main(void)
{
printf("Size of int = %u\n", sizeof(int));
printf("Size of char = %u\n", sizeof(char));
printf("Size of float = %u\n", sizeof(float));
printf("Size of double = %u\n", sizeof(double));
return 0;
}
Output:
Size of int = 4
Size of char = 1
Size of float = 4
Size of double = 8
Assignment Operator in C |
Index |
Comma Operator in C |
No comments:
Post a Comment