Bit operation in C programming

All QuestionsCategory: C LanguageBit operation in C programming
Anonymous asked 2 years ago
1 Answers
Anonymous answered 2 years ago

In the C programming language, bit operations are operations that are performed on the individual bits of a number. These operations can be used to manipulate the bits of a number in various ways, such as setting, clearing, flipping, or checking the value of individual bits.
Here are some common bit operations in C:

  • The bitwise AND operator (&) is used to perform a bitwise AND operation on two numbers. The result of the operation is a number in which the bits are set to 1 only if the corresponding bits in both numbers are 1. For example, the expression 5 & 3 evaluates to 1, since the binary representation of 5 is 101 and the binary representation of 3 is 011, and the result of the AND operation is 001.
  • The bitwise OR operator (|) is used to perform a bitwise OR operation on two numbers. The result of the operation is a number in which the bits are set to 1 if either of the corresponding bits in the two numbers is 1. For example, the expression 5 | 3 evaluates to 7, since the binary representation of 5 is 101 and the binary representation of 3 is 011, and the result of the OR operation is 111.
  • The bitwise XOR operator (^) is used to perform a bitwise exclusive OR operation on two numbers. The result of the operation is a number in which the bits are set to 1 if the corresponding bits in the two numbers are different. For example, the expression 5 ^ 3 evaluates to 6, since the binary representation of 5 is 101 and the binary representation of 3 is 011, and the result of the XOR operation is 110.
  • The bitwise NOT operator (~) is used to perform a bitwise NOT operation on a number. This operation flips the value of each bit in the number, so that 1s become 0s and 0s become 1s. For example, the expression ~5 evaluates to -6, since the binary representation of 5 is 00000101 and the result of the NOT operation is 11111010.