Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Bit Shifts: Left and Right | Bitwise Operators and Shifts
Bitwise Operations and Binary Logic in C

bookBit Shifts: Left and Right

You will often need to manipulate the individual bits within a number. Two of the most powerful tools for this are the left shift (<<) and right shift (>>) operators. A left shift moves all bits in a value to the left by a specified number of positions, filling the vacated rightmost bits with zeros. This has the effect of multiplying the original value by a power of two. In contrast, a right shift moves all bits to the right, filling in the leftmost bits. For unsigned integers, right shifting divides the value by a power of two, discarding any remainder.

For instance, shifting the binary value 0000 1010 (which is 10 in decimal) left by one position produces 0001 0100 (20 in decimal), effectively doubling the value. Shifting the same value right by one position results in 0000 0101 (5 in decimal), effectively halving it.

bit_shifts_demo.c

bit_shifts_demo.c

copy
12345678910111213141516
#include <stdio.h> int main() { unsigned int value = 10; // Binary: 0000 1010 printf("Original value: %u (binary: 0000 1010)\n", value); unsigned int left_shifted = value << 1; // Shift left by 1 printf("Left shift by 1: %u (binary: 0001 0100)\n", left_shifted); unsigned int right_shifted = value >> 1; // Shift right by 1 printf("Right shift by 1: %u (binary: 0000 0101)\n", right_shifted); return 0; }

While left and right shifts are simple in principle, there are important caveats to keep in mind. Shifting a value by more bits than its width (for example, shifting a 32-bit integer by 32 or more) produces undefined behavior. This means the result could be unpredictable and should always be avoided. Additionally, shifting signed integers, especially right shifts, can lead to unexpected results: the behavior of right-shifting negative signed integers depends on the implementation and may either fill leftmost bits with zeros or with ones (arithmetic vs logical shift). To avoid surprises, prefer shifting unsigned values unless you are certain of the outcome and the behavior of your compiler.

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 2

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

bookBit Shifts: Left and Right

Sveip for å vise menyen

You will often need to manipulate the individual bits within a number. Two of the most powerful tools for this are the left shift (<<) and right shift (>>) operators. A left shift moves all bits in a value to the left by a specified number of positions, filling the vacated rightmost bits with zeros. This has the effect of multiplying the original value by a power of two. In contrast, a right shift moves all bits to the right, filling in the leftmost bits. For unsigned integers, right shifting divides the value by a power of two, discarding any remainder.

For instance, shifting the binary value 0000 1010 (which is 10 in decimal) left by one position produces 0001 0100 (20 in decimal), effectively doubling the value. Shifting the same value right by one position results in 0000 0101 (5 in decimal), effectively halving it.

bit_shifts_demo.c

bit_shifts_demo.c

copy
12345678910111213141516
#include <stdio.h> int main() { unsigned int value = 10; // Binary: 0000 1010 printf("Original value: %u (binary: 0000 1010)\n", value); unsigned int left_shifted = value << 1; // Shift left by 1 printf("Left shift by 1: %u (binary: 0001 0100)\n", left_shifted); unsigned int right_shifted = value >> 1; // Shift right by 1 printf("Right shift by 1: %u (binary: 0000 0101)\n", right_shifted); return 0; }

While left and right shifts are simple in principle, there are important caveats to keep in mind. Shifting a value by more bits than its width (for example, shifting a 32-bit integer by 32 or more) produces undefined behavior. This means the result could be unpredictable and should always be avoided. Additionally, shifting signed integers, especially right shifts, can lead to unexpected results: the behavior of right-shifting negative signed integers depends on the implementation and may either fill leftmost bits with zeros or with ones (arithmetic vs logical shift). To avoid surprises, prefer shifting unsigned values unless you are certain of the outcome and the behavior of your compiler.

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 2
some-alt