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
35
votes
9 answers

Difference between SHL and SAL in 80x86

I have learned how to work with 80x86 assembler, so in bit-wise shift operation, I faced a problem with SAL and SHL usage. I means the difference between lines of code as follow : MOV X, 0AAH SAL X, 4 MOV X, 0AAH SHL X, 4 When we should use SHL…
Hossein Mobasher
  • 4,382
  • 5
  • 46
  • 73
35
votes
6 answers

Need help understanding "getbits()" method in Chapter 2 of K&R C

In chapter 2, the section on bitwise operators (section 2.9), I'm having trouble understanding how one of the sample methods works. Here's the method provided: unsigned int getbits(unsigned int x, int p, int n) { return (x >> (p + 1 - n)) & ~(~0…
John Rudy
  • 37,282
  • 14
  • 64
  • 100
35
votes
1 answer

unsigned right Shift '>>>' Operator in Java

Possible Duplicate: Why is (-1 >>> 32) = -1? The unsigned right shift operator inserts a 0 in the leftmost. So when I do System.out.println(Integer.toBinaryString(-1>>>30)) output 11 Hence, it is inserting 0 in the left most…
Keen Sage
  • 1,899
  • 5
  • 26
  • 44
34
votes
1 answer

Why was 1 << 31 changed to be implementation-defined in C++14?

In all versions of C and C++ prior to 2014, writing 1 << (CHAR_BIT * sizeof(int) - 1) caused undefined behaviour, because left-shifting is defined as being equivalent to successive multiplication by 2, and this shift causes signed integer…
M.M
  • 138,810
  • 21
  • 208
  • 365
32
votes
2 answers

What are the exact semantics of Rust's shift operators?

I tried to find exact information about how the << and >> operators work on integers, but I couldn't find a clear answer (the documentation is not that great in that regard). There are two parts of the semantics that are not clear to me. First,…
Lukas Kalbertodt
  • 79,749
  • 26
  • 255
  • 305
32
votes
2 answers

Arithmetic right shift gives bogus result?

I must be absolutely crazy here, but gcc 4.7.3 on my machine is giving the most absurd result. Here is the exact code that I'm testing: #include using namespace std; int main(){ unsigned int b = 100000; cout << (b>>b) << endl; b…
Suedocode
  • 2,504
  • 3
  • 23
  • 41
32
votes
5 answers

Declaring 64-bit variables in C

I have a question. uint64_t var = 1; // this is 000000...00001 right? And in my code this works: var ^ (1 << 43) But how does it know 1 should be in 64 bits? Shouldn’t I write this instead? var ^ ( (uint64_t) 1 << 43 )
David 天宇 Wong
  • 3,724
  • 4
  • 35
  • 47
31
votes
5 answers

Right shift and signed integer

On my compiler, the following pseudo code (values replaced with binary): sint32 word = (10000000 00000000 00000000 00000000); word >>= 16; produces a word with a bitfield that looks like this: (11111111 11111111 10000000 00000000) Can I rely on…
Anne Quinn
  • 12,609
  • 8
  • 54
  • 101
31
votes
6 answers

What's the purpose of the rotate instructions (ROL, RCL on x86)?

I always wondered what's the purpose of the rotate instructions some CPUs have (ROL, RCL on x86, for example). What kind of software makes use of these instructions? I first thought they may be used for encryption/computing hash codes, but these…
Gratian Lup
  • 1,485
  • 3
  • 19
  • 29
30
votes
4 answers

When to use Shift operators << >> in C#?

I was studying shift operators in C#, trying to find out when to use them in my code. I found an answer but for Java, you could: a) Make faster integer multiplication and division operations: *4839534 * 4* can be done like this: 4839534 << 2 or…
Junior Mayhé
  • 16,144
  • 26
  • 115
  • 161
29
votes
4 answers

Bitshift in javascript

I've got a really big number: 5799218898. And want to shift it right to 13 bits. So, windows-calculator or python gives me: 5799218898 >> 13 | 100010100100001110011111100001 >> 13 70791 | 10001010010000111 As expected. But…
Andrew
  • 8,330
  • 11
  • 45
  • 78
28
votes
3 answers

Is left and right shifting negative integers defined behavior?

I know, right shifting a negative signed type depends on the implementation, but what if I perform a left shift? For example: int i = -1; i << 1; Is this well-defined? I think the standard doesn't say about negative value with signed type if E1…
user103214
  • 3,478
  • 6
  • 26
  • 37
28
votes
3 answers

Arithmetic bitwise shift right "a shr b" with signed integers that stored in variables – wrong results! Internal Delphi’s bug?

I have a question (or more likely a bug report) about bit shifting behavior in Delphi (tested in Borland Delphi 7). Target: perform an "Arithmetic" bitwise shift right with any number. This means that sign-bit must be extended – binary number will…
aleksusklim
  • 361
  • 3
  • 11
28
votes
5 answers

Why does a shift by 0 truncate the decimal?

I recently found this piece of JavaScript code: Math.random() * 0x1000000 << 0 I understood that the first part was just generating a random number between 0 and 0x1000000 (== 16777216). But the second part seemed odd. What's the point of…
voithos
  • 68,482
  • 12
  • 101
  • 116
27
votes
3 answers

Java: right shift on negative number

I am very confused on right shift operation on negative number, here is the code. int n = -15; System.out.println(Integer.toBinaryString(n)); int mask = n >> 31; System.out.println(Integer.toBinaryString(mask)); And the result…
Cacheing
  • 3,431
  • 20
  • 46
  • 65