# Arithmetic and logical binary operations in python

The bitwise XOR may be used to invert selected bits in a register also called toggle or flip. Any bit may be toggled by XORing it with 1. 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 pattern is recognized by many compilers, and the compiler will emit a single rotate instruction: Clang provides some rotate intrinsics for Microsoft compatibility that suffers the problems above. Intel also provides x86 Intrinsics.

More details of Java shift operators: JavaScript uses bitwise operations to evaluate each of two or more units place to 1 or 0.

In Pascal, as well as in all its dialects such as Object Pascal and Standard Pascal , the left and right shift operators are " shl " and " shr ", respectively.

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. 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 pattern is recognized by many compilers, and the compiler will emit a single rotate instruction: Clang provides some rotate intrinsics for Microsoft compatibility that suffers the problems above. Intel also provides x86 Intrinsics. More details of Java shift operators: JavaScript uses bitwise operations to evaluate each of two or more units place to 1 or 0. In Pascal, as well as in all its dialects such as Object Pascal and Standard Pascal , the left and right shift operators are " shl " and " shr ", respectively.

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. August Learn how and when to remove this template message. Retrieved 12 August Use bitwise operators only on unsigned operands".

Retrieved 7 September Retrieved 14 July