C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Info: The integer is shifted right (>>) zero places, then one place, and then two and more places.
Tip: You can see that all numbers that are negative have the very leftmost (first) bit set to 1, while positive numbers have it set to 0.
Note: The output of the program illustrates how the bit values are changed with the parameters to the shift operators.
C# program that uses right shift, left shift
using System;
class Program
{
static void Main()
{
// This program shift an integer right.
// ... Then it shifts it left.
// ... It displays the bits and the decimal representation.
int value1 = 99999999;
for (int i = 0; i < 32; i++)
{
int shift = value1 >> i;
Console.WriteLine("{0} = {1}",
GetIntBinaryString(shift), shift);
}
for (int i = 0; i < 32; i++)
{
int shift = value1 << i;
Console.WriteLine("{0} = {1}",
GetIntBinaryString(shift), shift);
}
}
static string GetIntBinaryString(int value)
{
// From other article.
return Convert.ToString(value, 2).PadLeft(32, '0');
}
}
Output
00000101111101011110000011111111 = 99999999
00000010111110101111000001111111 = 49999999
00000001011111010111100000111111 = 24999999
00000000101111101011110000011111 = 12499999
00000000010111110101111000001111 = 6249999
00000000001011111010111100000111 = 3124999
00000000000101111101011110000011 = 1562499
00000000000010111110101111000001 = 781249
00000000000001011111010111100000 = 390624
00000000000000101111101011110000 = 195312
00000000000000010111110101111000 = 97656
00000000000000001011111010111100 = 48828
00000000000000000101111101011110 = 24414
00000000000000000010111110101111 = 12207
00000000000000000001011111010111 = 6103
00000000000000000000101111101011 = 3051
00000000000000000000010111110101 = 1525
00000000000000000000001011111010 = 762
00000000000000000000000101111101 = 381
00000000000000000000000010111110 = 190
00000000000000000000000001011111 = 95
00000000000000000000000000101111 = 47
00000000000000000000000000010111 = 23
00000000000000000000000000001011 = 11
00000000000000000000000000000101 = 5
00000000000000000000000000000010 = 2
00000000000000000000000000000001 = 1
00000000000000000000000000000000 = 0
00000000000000000000000000000000 = 0
00000000000000000000000000000000 = 0
00000000000000000000000000000000 = 0
00000000000000000000000000000000 = 0
00000101111101011110000011111111 = 99999999
00001011111010111100000111111110 = 199999998
00010111110101111000001111111100 = 399999996
00101111101011110000011111111000 = 799999992
01011111010111100000111111110000 = 1599999984
10111110101111000001111111100000 = -1094967328
01111101011110000011111111000000 = 2105032640
11111010111100000111111110000000 = -84902016
11110101111000001111111100000000 = -169804032
11101011110000011111111000000000 = -339608064
11010111100000111111110000000000 = -679216128
10101111000001111111100000000000 = -1358432256
01011110000011111111000000000000 = 1578102784
10111100000111111110000000000000 = -1138761728
01111000001111111100000000000000 = 2017443840
11110000011111111000000000000000 = -260079616
11100000111111110000000000000000 = -520159232
11000001111111100000000000000000 = -1040318464
10000011111111000000000000000000 = -2080636928
00000111111110000000000000000000 = 133693440
00001111111100000000000000000000 = 267386880
00011111111000000000000000000000 = 534773760
00111111110000000000000000000000 = 1069547520
01111111100000000000000000000000 = 2139095040
11111111000000000000000000000000 = -16777216
11111110000000000000000000000000 = -33554432
11111100000000000000000000000000 = -67108864
11111000000000000000000000000000 = -134217728
11110000000000000000000000000000 = -268435456
11100000000000000000000000000000 = -536870912
11000000000000000000000000000000 = -1073741824
10000000000000000000000000000000 = -2147483648
Tip: If you are using a data structure that uses bitmasks, bit shifting can be used to guide the control flow.
GetHashCodeDivide by Powers of Two