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
16
votes
4 answers

What does a >> mean in Go language?

I’m looking for information on Google’s Go language. In “A Tour of Go” they have this code: const ( Big = 1<<100 Small = Big>>99 ) But what do << and >> mean? You can see all of the code at http://tour.golang.org/#14
DotDot
  • 653
  • 1
  • 7
  • 15
16
votes
2 answers

Does C++20 well-define left shift for signed integers that "overflow"?

In the current C++ Standard Draft, the left shift operator is defined as follows [expr.shift]: The value of E1 << E2 is the unique value congruent to E1×2^E2 modulo 2^N, where N is the width of the type of the result. Consider int E1 = 2^31-1 =…
Daniel Langr
  • 22,196
  • 3
  • 50
  • 93
16
votes
2 answers

Undefined behavior of right-shift in C++

From cppreference.com: For unsigned a and for signed a with nonnegative values, the value of a >> b is the integer part of a/2b . For negative a, the value of a >> b is implementation-defined (in most implementations, this performs arithmetic…
R2B2
  • 1,541
  • 1
  • 12
  • 19
16
votes
7 answers

How to shift an array of bytes by 12-bits

I want to shift the contents of an array of bytes by 12-bit to the left. For example, starting with this array of type uint8_t shift[10]: {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A, 0xBC} I'd like to shift it to the left by 12-bits…
Justin Tanner
  • 14,062
  • 17
  • 82
  • 103
16
votes
3 answers

What's the reason high-level languages like C#/Java mask the bit shift count operand?

This is more of a language design rather than a programming question. The following is an excerpt from JLS 15.19 Shift Operators: If the promoted type of the left-hand operand is int, only the five lowest-order bits of the right-hand operand are…
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
16
votes
1 answer

How are shifts implemented on the hardware level?

How are bit shifts implemented at the hardware level when the number to shift by is unknown? I can't imagine that there would be a separate circuit for each number you can shift by (that would 64 shift circuits on a 64-bit machine), nor can I…
Matt
  • 21,026
  • 18
  • 63
  • 115
15
votes
4 answers

Signed right shift: which compiler use logical shift

I tested right shift with Visual Studio, Ubuntu's GCC, Intel compiler, MinGW. All shift in the sign bit. I guess Xcode's GCC does the same. I know that the behavior is implementation specific, but it looks like that all major desktop/server…
pic11
  • 14,267
  • 21
  • 83
  • 119
15
votes
3 answers

Can you bitwise shift a bool in C++?

I'm using someone else's code that was written with an older compiler that mapped a special BOOL type to an unsigned int, but in my compiler it's mapped to a true bool. In some places in his code he uses the bitwise shift operator << on the bool…
Phlucious
  • 3,704
  • 28
  • 61
15
votes
5 answers

Shifting the sign bit in .NET

I'm reading bits from a monochrome bitmap. I'm storing every 16 bits in a short in the reverse order. If the bit in the bitmap is black, store a 1. If white, store a 0. E.g.: for bitmap: bbbw bbbw bbbw wwww my short is: 0000 0111 0111 0111 The 1st…
Dinah
  • 52,922
  • 30
  • 133
  • 149
15
votes
5 answers

Is Shifting more than 32 bits of a uint64_t integer on an x86 machine Undefined Behavior?

Learning the hard way, I tried to left shift a long long and uint64_t to more than 32 bits on an x86 machine resulted 0. I vaguely remember to have read somewhere than on a 32 bit machine shift operators only work on the first 32 bits but cannot…
Abhijit
  • 62,056
  • 18
  • 131
  • 204
14
votes
2 answers

Why does Java `BitSet` not have `shiftLeft` and `shiftRight` functions?

Is there any particular reason why these are missing? They do exist in BigInteger, but due to the immutable design pattern of BigInteger these are usually awfully slow. BitSet is much nicer because it is mutable, but I really miss the shift…
Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194
14
votes
3 answers

Why use only the lower five bits of the shift operand when shifting a 32-bit value? (e.g. (UInt32)1 << 33 == 2)

Consider the following code: UInt32 val = 1; UInt32 shift31 = val << 31; // shift31 == 0x80000000 UInt32 shift32 = val << 32; // shift32 == 0x00000001 UInt32 shift33 = val << 33; // shift33 …
Daniel LeCheminant
  • 50,583
  • 16
  • 120
  • 115
14
votes
1 answer

Why is (2^31) >> 32 not 0?

My problem is that np.array([2**31], dtype=np.uint32) >> 32 does not return 0, but returns array([2147483648], dtype=uint32) instead. The same is true for np.right_shift(np.array([2**31], dtype=np.uint32), 32) (so I believe this is simply how >>…
bers
  • 4,817
  • 2
  • 40
  • 59
14
votes
5 answers

re implement modulo using bit shifts?

I'm writing some code for a very limited system where the mod operator is very slow. In my code a modulo needs to be used about 180 times per second and I figured that removing it as much as possible would significantly increase the speed of my…
PgrAm
  • 671
  • 2
  • 7
  • 19
13
votes
2 answers

Java bitwise operator <<

Can someone explain why the following bitwise expressions return different results: System.out.println((-1<<31)<<1); // it prints 0 System.out.println(-1<<32); // it prints -1
nenito
  • 1,214
  • 6
  • 19
  • 33