Bitwise operations are operations used to perform manipulations on bit patterns or binary numbers, which include working with individual bits. This quick, simple action, directly supported by the processor, is used to manage values for comparisons and calculations.
Basis of Computing
The binary digital system uses only two digits - 0 and 1. Computers work in binary format, which means that they store data and perform calculations using only zeros and ones.
Although a single binary digit can be used to represent True (1) (true) or False (0) (false) in the logic, several binary digits can be used to store large numbers and perform complex functions. In fact, any number can be represented in binary format.
Application
Bitwise operators are used in the following areas:
Communication stacks, where the individual bits in the header attached to the data carry important information.
Embedded software for controlling various functions in the chip and indicating hardware status by controlling individual bits of the hardware registers of the embedded microcontrollers.
Low-level programming for applications such as device drivers, cryptographic software, video decoding software, memory allocators, compression and graphics software.
Convenient maintenance of large sets of integers in search and optimization problems.
Bitwise operations performed with bit flags, which can include an instance of an enumeration type to store any combination of values defined in the list of enumerators.
Bitwise operations - how does it work?
Unlike ordinary logical operators (for example, +, -, *) that work with bytes or groups of bytes, bitwise operators can check or set each of the individual bits in a byte. Bitwise operations never cause an overflow in memory cells, because the result obtained after the operation is within the range of possible values for a numeric type.
Bitwise operators used in the C language family (C #, C, and C ++):
OR (|) - the result is true if any of the operands is true.
AND (&) - the result is true only if both operands are true. It can be used to configure a mask for checking values of certain bits.
XOR (^) - the result is true only if one of its operands is true. It is used mainly to switch certain bits. It also helps replace two variables without using a third.
NOT (~) - bitwise complement or inversion. Provides a bitwise complement to the operand by inverting its value, so that all zeros are converted to units, and all units are converted to zeros.
>> (Right-Shift) and << (Left-Shift) - an operator that moves bits to the number of positions specified by the second operand in the right or left direction. Shift operators are used to align bits.
Work example
Bitwise operators are symbols that represent actions that must be performed on individual bits. The bitwise operation works on two-bit patterns of the same length, positioning their individual bits:
The logical operation AND (&) of each bit pair leads to 1 (true) if the first and second bits are equal to 1. Otherwise, the result is zero. Among other uses, AND can be used to check for individual bits in a bit string to see if they are false or true.
Let’s take a closer look at an example:
IsOdd = (ValueToTest & 1)! = 0.
The logical operation OR (|) of each bit pair leads to 1 if the first or second bit is 1. Otherwise, the result is zero. The logical operation XOR (~) of each bit pair results in 1 if the two bits are different, and 0 if they are the same.
The logical NOT operator is represented as ^. The left shift (<<), the right shift (>>), and the right zero fill shift (>>>>) are sometimes referred to as bitwise operators and are called bit shift operators.
Prioritization
The order of precedence (from highest to lowest) in bitwise operators when programming in C:
These operands are used in most programming languages. For example, when writing code in Javascript, the bitwise operations will be the same as the above. This is caused by the basic mathematical foundations on which the program code is based. In particular, bitwise operations in Java, in turn, are completely identical to Javascript.
Bit and programming in C and other languages
Bit is the smallest unit of measurement used to quantify computer data. It contains one binary value - 0 or 1.
Although one bit can determine the logical value True (1) or False (0), it is rarely used as a separate unit. Therefore, in computer storage, bits are often grouped into 8-bit clusters called bytes. Since a byte contains eight bits, each of which has two possible values, in bitwise operations in C (programming language), one byte can have 28 or 256 different values.
The terms “bits” and “bytes” are often confused and even used interchangeably, because they sound the same and both are abbreviated by the letter “B”. However, if spelled correctly, bits are truncated in lowercase "b", and bytes are truncated in upper case - "B". It is important not to confuse these two terms, since any measurement in bytes contains eight times as many bits. For example, a small 4 KB text file contains 4,000 bytes or 32,000 bits.
Typically, files, storage devices, and storage capacity are measured in bytes, and data transfer rates are measured in bits. For example, an SSD memory card can have a capacity of 240 GB, while downloading can be carried at a speed of 10 Mbps. In addition, bits are also used to describe processor architecture, such as a 32-bit or 64-bit processor.
Pascal bitwise operations
The bitwise level of operations in pascal includes working with individual bits, which are the smallest units of data on a computer. Although computers are capable of manipulating bits, they usually store data and execute instructions in bitwise multiple values called bytes. Most programming languages, including bitwise operations in Delphi, control groups of 8, 16, or 32 bits.
Process description
A bitwise operator is a character that represents an action that works with data at the bit level, rather than with bytes or large units of data, as is more common.
Most common operators work with one or more bytes, which on most systems contain eight bits. Because they provide greater accuracy and require fewer resources, bitwise operators can make code faster and more efficient. Examples of using bitwise operations include:
encryption;
compression;
schedule;
communication on ports / sockets;
embedded system programming;
machines with a final state.
The bitwise operator works with the binary representation of a number, not its value. The operand is considered as a set of bits, and not as a single number. Bitwise operators are similar in most of the languages that support them - C, Java, JavaScript, Python, and Visual Basic.
Why is it important to use?
Bitwise operations are absolutely necessary when programming hardware registers in embedded systems. Each processor has one or more registers (usually a specific memory address) that control whether the interrupt is on or off. To allow an interrupt to start a regular process, you must set the enable bit for this type of interrupt, and most importantly, do not change any of the other bits in the register.
When an interrupt is triggered, it usually sets a bit in the status register, so that one service procedure can determine the exact cause of the interrupt. Testing individual bits allows you to quickly decode an interrupt source.
On many embedded systems, the total available RAM may be 64, 128, or 256 bytes. In this environment, a single byte is usually used to store several data elements and Boolean flags, and then bit operations are used to set and read them.