Questions tagged [bit-shift]

A bit shift operation moves the bits contained in a binary numeral or bit pattern to the left or to the right.

A bit shift operation moves the bits contained in a binary numeral or bit pattern to the left or to the right, often written as << and >> respectively.

For example 4<<3 = 32 and 5>>1 = 2.

A zero shift returns the integer part, e.g. 3.14<<0 = 3.

2051 questions
1539
votes
10 answers

What are bitwise shift (bit-shift) operators and how do they work?

I've been attempting to learn C in my spare time, and other languages (C#, Java, etc.) have the same concept (and often the same operators)... At a core level, what does bit-shifting (<<, >>, >>>) do, what problems can it help solve, and what…
329
votes
19 answers

Is multiplication and division using shift operators in C actually faster?

Multiplication and division can be achieved using bit operators, for example i*2 = i<<1 i*3 = (i<<1) + i; i*10 = (i<<3) + (i<<1) and so on. Is it actually faster to use say (i<<3)+(i<<1) to multiply with 10 than using i*10 directly? Is there any…
eku
  • 3,357
  • 3
  • 19
  • 20
296
votes
3 answers

What is the >>>= operator in C?

Given by a colleague as a puzzle, I cannot figure out how this C program actually compiles and runs. What is this >>>= operator and the strange 1P1 literal? I have tested in Clang and GCC. There are no warnings and the output is "???" #include…
CustomCalc
  • 2,182
  • 3
  • 12
  • 9
177
votes
11 answers

Are the shift operators (<<, >>) arithmetic or logical in C?

In C, are the shift operators (<<, >>) arithmetic or logical?
littlebyte
  • 1,789
  • 2
  • 11
  • 5
172
votes
7 answers

What is the JavaScript >>> operator and how do you use it?

I was looking at code from Mozilla that add a filter method to Array and it had a line of code that confused me. var len = this.length >>> 0; I have never seen >>> used in JavaScript before. What is it and what does it do?
Kenneth J
  • 4,846
  • 11
  • 39
  • 56
170
votes
9 answers

How does this print "hello world"?

I discovered this oddity: for (long l = 4946144450195624l; l > 0; l >>= 5) System.out.print((char) (((l & 31 | 64) % 95) + 32)); Output: hello world How does this work?
Bohemian
  • 412,405
  • 93
  • 575
  • 722
162
votes
1 answer

Times-two faster than bit-shift, for Python 3.x integers?

I was looking at the source of sorted_containers and was surprised to see this line: self._load, self._twice, self._half = load, load * 2, load >> 1 Here load is an integer. Why use bit shift in one place, and multiplication in another? It seems…
158
votes
9 answers

How do shift operators work in Java?

I am trying to understand the shift operators and couldn't get much. When I tried to execute the below code System.out.println(Integer.toBinaryString(2 << 11)); System.out.println(Integer.toBinaryString(2 <<…
gnreddy
  • 2,393
  • 4
  • 22
  • 18
84
votes
5 answers

Bitwise operators and "endianness"

Does endianness matter at all with the bitwise operations? Either logical or shifting? I'm working on homework with regard to bitwise operators, and I can not make heads or tails on it, and I think I'm getting quite hung up on the endianess. That…
Frank V
  • 25,141
  • 34
  • 106
  • 144
83
votes
42 answers

Have you ever had to use bit shifting in real projects?

Have you ever had to use bit shifting in real programming projects? Most (if not all) high level languages have shift operators in them, but when would you actually need to use them?
Philip Morton
  • 129,733
  • 38
  • 88
  • 97
81
votes
14 answers

Java: Checking if a bit is 0 or 1 in a long

What method would you use to determine if the the bit that represents 2^x is a 1 or 0 ?
Ande Turner
  • 7,096
  • 19
  • 80
  • 107
75
votes
10 answers

Why doesn't left bit-shift, "<<", for 32-bit integers work as expected when used more than 32 times?

When I write the following program and use the GNU C++ compiler, the output is 1 which I think is due to the rotation operation performed by the compiler. #include int main() { int a = 1; std::cout << (a << 32) << std::endl; …
AnkitSablok
  • 3,021
  • 7
  • 35
  • 52
72
votes
6 answers

Arithmetic bit-shift on a signed integer

I am trying to figure out how exactly arithmetic bit-shift operators work in C, and how it will affect signed 32-bit integers. To make things simple, let's say we work within one byte (8 bits): x = 1101.0101 MSB[ 1101.0101 ]LSB Reading other posts…
newprint
  • 6,936
  • 13
  • 67
  • 109
72
votes
5 answers

What is (x & 1) and (x >>= 1)?

I am trying to do assignment: "Find the number of bits in an unsigned integer data type without using the sizeof() function." And my design is to convert the integer to bits and then to count them. For ex: 10 is 1010 and 5 is 101 Converting integer…
Sandra K
  • 1,209
  • 1
  • 10
  • 21
71
votes
6 answers

warning: left shift count >= width of type

I'm very new to dealing with bits and have got stuck on the following warning when compiling: 7: warning: left shift count >= width of type My line 7 looks like this unsigned long int x = 1 << 32; This would make sense if the size of long on my…
Rupert Madden-Abbott
  • 12,899
  • 14
  • 59
  • 74
1
2 3
99 100