![Forrest logo](/images/logo.png)
bitwise:tldr:aada4
The term "bitwise" refers to a type of operation that operates on individual bits of a binary representation of numerical values. In computer programming, bitwise operations are commonly performed at the binary level, manipulating individual bits by using logic gates.
Bitwise operations include logical AND, logical OR, logical XOR, and logical NOT. These operations allow programmers to manipulate and extract specific bits from numbers efficiently.
Here's a brief explanation of each bitwise operation:
-
Logical AND (&): Performs a bitwise "and" operation between two numbers. Each corresponding bit of the operands is combined using the following rule: if both bits are 1, the result is 1; otherwise, the result is 0.
-
Logical OR (|): Performs a bitwise "or" operation between two numbers. Each corresponding bit of the operands is combined using the following rule: if either bit is 1, the result is 1; otherwise, the result is 0.
-
Logical XOR (^): Performs a bitwise "exclusive or" operation between two numbers. Each corresponding bit of the operands is combined using the following rule: if exactly one bit is 1, the result is 1; otherwise, the result is 0.
-
Logical NOT (~): Performs a bitwise "not" operation on a single number. Each bit of the number is inverted, meaning 0 becomes 1 and 1 becomes 0.
These bitwise operations can be used for various purposes, such as manipulating individual bits in binary values, implementing flags or toggle switches, or performing optimizations in certain algorithms, among others.