Bit shifting is an operation done on all the bits of a binary value in which they are moved by a determined number of places to either the left or right. Bit shifting is used when the operand is being used as a series of bits rather than as a whole. In other words, the operand is treated as individual bits that stand for something and not as a value.
Bit shifting is often used in programming and has at least one variation in each programming language.
Bit shifting may also be known as a bitwise operation.
There are two variations to bit shifting, shift right and shift left, and it is further defined by the number of places in which the shift should occur. For example, shifting the operand one value to the left or shifting the bits "n" values to the right.
There are also two kinds of bit shifting, logical and arithmetic. Logical bit shifting may be useful for multiplying or dividing unsigned integers by powers of two. For example, if the value "0001" or "1" is shifted left, it becomes "0010" or "2," shifted to the left again it becomes "0100," or "4." Shifting to the right has an opposite effect of dividing the value by two per shift. In most cases, shifting is treated as circular so when shifting to the left, the leftmost value becomes the rightmost value, and vice versa.
Logical left shift and arithmetic left shift have the same effect so Java only has a single left shift operator (<<). The arithmetic right shift is (>>) while the logical is (>>>). In C and C++, there is only one shift right operator (>>); the kind of shift to be done is determined by the type of integer being shifted. Signed integers are shifted using arithmetic while logical bit shifting is used on unsigned integers. Bit shifting is also used a lot in assembly programming because microcontrollers and microprocessors usually rely on flags, which are represented by individual bits. Basically, it's because the binary number system is used in programming in assembly language that bit shifting becomes a commonly used operator.
0 Comments