# Arithmetic and logical binary operators

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:. Division is a particularly complicated operation, and most programmable chips do not have dedicated divider modules. There are a number of logical operators. Logical operators act on an entire value multiple bits , and treat the values of "zero" as being "false", and "non-zero" as being "true".

What happens is that Verilog converts the whole number into either a 1 if it contains a nonzero bit or 0 if it only contains zero , then performs the equivalent bitwise operation. Thus, the answer is also one bit. There are a number of bitwise operators to perform boolean operations on each individual bit in a bus. The continuous assignment is typically used with wires and other structures that do not have memory.

A continuous assignment is happening continuously and in parallel to all other computational tasks. Do whatever should happen when dividing by zero. Explanation To understand how the division algorithm works, consider this simple algorithm: You simply measure how many times the divisor can be subtracted from the dividend before it becomes negative.

However, suppose your dividend is and your divisor is 2. This algorithm will loop for iterations. How to improve it? Well, you could first divide by sixteen, i. But we could do better! How about dividing first by ? And maybe we could subdivide that 16 into 4 and 8. The first algorithm automates this subdivision process, doing the compare-and-subtract test for all power-of-two multiples of the divisor, converging towards the O log2 n goal. It begins with the largest multiple of the divisor that is not larger than the dividend.

Signed division can be accomplished by doing an unsigned division and taking manually care of the negative-signs. Signed modulo can be accomplished by calculating an unsigned modulo and taking manually care of the negative-signs. Do NOT toggle the signed flag. For positive integer exponents: Multiplication Subtraction Unsigned greater-than comparison Random access memory or a stack Note that you must know the upper limit of the exponents beforehand.

It is not necessary to use the extra memory or stack or temporary variables, but then you would need to recalculate the same values many times. It is still more efficient than a multiplication loop if the multiplier is large enough and has few bits set.

If you know the exponent beforehand, there is a slightly more efficient way to subdivide the exponentiation, especially if the exponent is large. The GCC source code gives this reference for the algorithm: Knuth, "Seminumerical Algorithms", Vol. It means the following: Equality comparison Equality can be tested in various ways: Non-equality can be tested in various ways: