Logical shift

From WikiMD's Wellness Encyclopedia

Logical Shift[edit | edit source]

A logical shift is a type of bitwise operation that is commonly used in computer programming and digital logic circuits. It involves shifting the bits of a binary number to the left or right, while preserving the sign of the number. Logical shifts are often used for efficient multiplication or division by powers of two, as well as for manipulating and extracting specific bits within a binary number.

Basic Operation[edit | edit source]

The basic operation of a logical shift involves moving each bit in a binary number a certain number of positions to the left or right. The shifted bits are then replaced with zeros. The direction of the shift, either left or right, determines whether the number is multiplied or divided by a power of two.

In a left logical shift, each bit is shifted to the left by a specified number of positions. The leftmost bit is discarded, and a zero is inserted at the rightmost position. This effectively multiplies the number by 2 raised to the power of the shift amount.

In a right logical shift, each bit is shifted to the right by a specified number of positions. The rightmost bit is discarded, and zeros are inserted at the leftmost positions. This effectively divides the number by 2 raised to the power of the shift amount.

Applications[edit | edit source]

Logical shifts have various applications in computer programming and digital logic circuits. Some common applications include:

- Efficient multiplication or division by powers of two: Since a left logical shift multiplies a number by a power of two, it can be used to perform fast multiplication. Similarly, a right logical shift can be used for efficient division by a power of two.

- Bit manipulation: Logical shifts are often used to manipulate and extract specific bits within a binary number. By shifting the bits to the desired position, bitwise operations such as AND, OR, and XOR can be performed to modify or extract specific bits.

- Data compression: Logical shifts are used in various data compression algorithms, such as Huffman coding and run-length encoding. By shifting and manipulating the bits of the data, compression techniques can be applied to reduce the size of the data representation.

Example[edit | edit source]

Let's consider an example to illustrate the concept of logical shift. Suppose we have a binary number 10101010 and we want to perform a left logical shift by 2 positions.

Original number: 10101010 Left logical shift by 2: 10101000

As we can see, the two leftmost bits are discarded, and two zeros are inserted at the rightmost positions. This effectively multiplies the original number by 2 raised to the power of 2, resulting in the shifted number.

See Also[edit | edit source]

- Bitwise operation - Binary number - Digital logic circuits

References[edit | edit source]

WikiMD
Navigation: Wellness - Encyclopedia - Health topics - Disease Index‏‎ - Drugs - World Directory - Gray's Anatomy - Keto diet - Recipes

Search WikiMD

Ad.Tired of being Overweight? Try W8MD's physician weight loss program.
Semaglutide (Ozempic / Wegovy and Tirzepatide (Mounjaro / Zepbound) available.
Advertise on WikiMD

WikiMD's Wellness Encyclopedia

Let Food Be Thy Medicine
Medicine Thy Food - Hippocrates

Medical Disclaimer: WikiMD is not a substitute for professional medical advice. The information on WikiMD is provided as an information resource only, may be incorrect, outdated or misleading, and is not to be used or relied on for any diagnostic or treatment purposes. Please consult your health care provider before making any healthcare decisions or for guidance about a specific medical condition. WikiMD expressly disclaims responsibility, and shall have no liability, for any damages, loss, injury, or liability whatsoever suffered as a result of your reliance on the information contained in this site. By visiting this site you agree to the foregoing terms and conditions, which may from time to time be changed or supplemented by WikiMD. If you do not agree to the foregoing terms and conditions, you should not enter or use this site. See full disclaimer.
Credits:Most images are courtesy of Wikimedia commons, and templates Wikipedia, licensed under CC BY SA or similar.

Contributors: Prab R. Tumpati, MD