Re anding 2 binary values
Bitwise AND is a really really useful tool for extracting bits from a number--you often create a "mask" value with 1's marking the bits you want, and AND by the mask.
For example, this code figures out if bit 2 of an integer is set: Be sure to use extra parenthesis! In assembly, it's the "and" instruction. Bitwise OR is useful for sticking together bit fields you've prepared separately.
Note how the low bit is 0, because both input bits are 1. The second property, that XOR by 1 inverts the value, is useful for flipping a set of bits. Output bits are 1 if the corresponding input bit is zero. The number of leading ones depends on the size of the machine's "int". I don't use bitwise NOT very often, but it's handy for making an integer whose bits are all 1: Internally, these operators map multi-bit values to a single bit by treating zero as a zero bit, and nonzero values as a one bit.
You've got to search all the HTML pages on the net for any possible word. One way to do this is for each possible word, store a giant table of every HTML document on the net maybe 10 billion documents containing one bit per document: This table is 10 billion bits, about 1GB uncompressed, or only a few dozen megabytes compressed.
Given two search words, you can find all the pages that contain both words by ANDing both tables. The output of the bitwise AND, where both bits are set to 1, is a new table listing the HTML pages that contain both search terms; now sort by pagerank, and you're done!
For example, given the bit pattern decimal 2 the second and fourth bits may be toggled by a bitwise XOR with a bit pattern containing 1 in the second and fourth positions:. Assembly language programmers and optimizing compilers sometimes use XOR as a short-cut to setting the value of a register to zero.
Performing XOR on a value against itself always yields zero, and on many architectures this operation requires fewer clock cycles and memory than loading a zero value and saving it to the register. The bit shifts are sometimes considered bitwise operations, because they treat a value as a series of bits rather than as a numerical quantity.
In these operations the digits are moved, or shifted , to the left or right. Registers in a computer processor have a fixed width, so some bits will be "shifted out" of the register at one end, while the same number of bits are "shifted in" from the other end; the differences between bit shift operators lie in how they determine the values of the shifted-in bits.
In an arithmetic shift , the bits that are shifted out of either end are discarded. In a left arithmetic shift, zeros are shifted in on the right; in a right arithmetic shift, the sign bit the MSB in two's complement is shifted in on the left, thus preserving the sign of the operand.
In the first case, the leftmost digit was shifted past the end of the register, and a new 0 was shifted into the rightmost position. In the second case, the rightmost 1 was shifted out perhaps into the carry flag , and a new 1 was copied into the leftmost position, preserving the sign of the number. Multiple shifts are sometimes shortened to a single shift by some number of digits.
A left arithmetic shift by n is equivalent to multiplying by 2 n provided the value does not overflow , while a right arithmetic shift by n of a two's complement value is equivalent to dividing by 2 n and rounding toward negative infinity.
If the binary number is treated as ones' complement , then the same right-shift operation results in division by 2 n and rounding toward zero. In a logical shift , zeros are shifted in to replace the discarded bits.
Therefore, the logical and arithmetic left-shifts are exactly the same. However, as the logical right-shift inserts value 0 bits into the most significant bit, instead of copying the sign bit, it is ideal for unsigned binary numbers, while the arithmetic right-shift is ideal for signed two's complement binary numbers.
Another form of shift is the circular shift or bit rotation. In this operation, the bits are "rotated" as if the left and right ends of the register were joined. The value that is shifted in on the right during a left-shift is whatever value was shifted out on the left, and vice versa. This operation is useful if it is necessary to retain all the existing bits, and is frequently used in digital cryptography. Rotate through carry is similar to the rotate no carry operation, but the two ends of the register are separated by the carry flag.
The bit that is shifted in on either end is the old value of the carry flag, and the bit that is shifted out on the other end becomes the new value of the carry flag. A single rotate through carry can simulate a logical or arithmetic shift of one position by setting up the carry flag beforehand.
For this reason, some microcontrollers such as low end PICs just have rotate and rotate through carry , and don't bother with arithmetic or logical shift instructions.
Rotate through carry is especially useful when performing shifts on numbers larger than the processor's native word size , because if a large number is stored in two registers, the bit that is shifted off the end of the first register must come in at the other end of the second.
With rotate-through-carry, that bit is "saved" in the carry flag during the first shift, ready to shift in during the second shift without any extra preparation.
The number of places to shift is given as the second argument to the shift operators. Shifts can result in implementation-defined behavior or undefined behavior , so care must be taken when using them. If the first operand is of type uint or ulong, the right-shift is a logical shift. The C-family of languages lack a rotate operator, but one can be synthesized from the shift operators. Care must be taken to ensure the statement is well formed to avoid undefined behavior and timing attacks in software with security requirements.
A second try might result in:. However, the branch adds an additional code path and presents an opportunity for timing analysis and attack, which is often not acceptable in high integrity software. To avoid the undefined behavior and branches under GCC and Clang, the following should be used.
The number of places to shift is given as the second argument. For example, the following assigns x the result of shifting y to the left by two bits:. Bitwise operations are necessary particularly in lower-level programming such as device drivers, low-level graphics, communications protocol packet assembly, and decoding.
Although machines often have efficient built-in instructions for performing arithmetic and logical operations, all these operations can be performed by combining the bitwise operators and zero-testing in various ways. Another example is a pseudocode implementation of addition, showing how to calculate a sum of two integers a and b using bitwise operators and zero-testing:.
From Wikipedia, the free encyclopedia. This article needs additional citations for verification. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed.