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

What does a bitwise shift (left or right) do and what is it used for?

I've seen the operators >> and << in various code that I've looked at (none of which I actually understood), but I'm just wondering what they actually do and what some practical uses of them are. If the shifts are like x * 2 and x / 2, what is the…
The.Anti.9
  • 43,474
  • 48
  • 123
  • 161
60
votes
7 answers

What does AND 0xFF do?

In the following code: short = ((byte2 << 8) | (byte1 & 0xFF)) What is the purpose of & 0xFF? Because sometimes, I see the above code written as: short = ((byte2 << 8) | byte1) And that seems to work fine too.
Maestro
  • 9,046
  • 15
  • 83
  • 116
57
votes
3 answers

The difference between logical shift right, arithmetic shift right, and rotate right

I've been reading the classic Hacker's delight and I am having trouble understanding the difference between logical shift right,arithmetic shift right, and rotate right. Please excuse if the doubt seems too simple.
Chandrahas Aroori
  • 955
  • 2
  • 14
  • 27
55
votes
8 answers

Why does left shift operation invoke Undefined Behaviour when the left side operand has negative value?

In C bitwise left shift operation invokes Undefined Behaviour when the left side operand has negative value. Relevant quote from ISO C99 (6.5.7/4) The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are filled with…
Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
46
votes
5 answers

c get nth byte of integer

I know you can get the first byte by using int x = number & ((1<<8)-1); or int x = number & 0xFF; But I don't know how to get the nth byte of an integer. For example, 1234 is 00000000 00000000 00000100 11010010 as 32bit integer How can I get all…
Kamran224
  • 1,584
  • 7
  • 20
  • 33
45
votes
8 answers

Java "Bit Shifting" Tutorial?

I would be thankful for a good tutorial, that explain for java newbies how in java all the "bit shifting" work. I always stumble across it, but never understood how it works. It should explain all the operations and concepts that are possible with…
Markus
  • 4,062
  • 4
  • 39
  • 42
44
votes
7 answers

Is bit shifting O(1) or O(n)?

Are shift operations O(1) or O(n) ? Does it make sense that computers generally require more operations to shift 31 places instead of shifting 1 place? Or does it make sense the number of operations required for shifting is constant regardless of…
Pacerier
  • 86,231
  • 106
  • 366
  • 634
44
votes
9 answers

Why use the Bitwise-Shift operator for values in a C enum definition?

Apple sometimes uses the Bitwise-Shift operator in their enum definitions. For example, in the CGDirectDisplay.h file which is part of Core Graphics: enum { kCGDisplayBeginConfigurationFlag = (1 << 0), kCGDisplayMovedFlag = (1 << 1), …
Dave
  • 12,408
  • 12
  • 64
  • 67
43
votes
15 answers

What do two left-angle brackets "<<" mean in C#?

Basically the questions in the title. I'm looking at the MVC 2 source code: [Flags] public enum HttpVerbs { Get = 1 << 0, Post = 1 << 1, Put = 1 << 2, Delete = 1 << 3, Head = 1 << 4 } and I'm just curious as to what the double…
lancscoder
  • 8,658
  • 8
  • 48
  • 68
43
votes
6 answers

Right shifting negative numbers in C

I have C code in which I do the following. int nPosVal = +0xFFFF; // + Added for ease of understanding int nNegVal = -0xFFFF; // - Added for valid reason Now when I try printf ("%d %d", nPosVal >> 1, nNegVal >> 1); I get 32767 -32768 Is this…
Alphaneo
  • 12,079
  • 22
  • 71
  • 89
41
votes
3 answers

Why does 11010100 << 1 equal 110101000, not 10101000?

Why when I try to shift bits for 110101002, the result is 1101010002, not 101010002. int a = Integer.parseInt("11010100", 2) << 1; I try to do this: int a = (byte)(Integer.parseInt("11010100", 2) << 1); But if the output value is greater than 128,…
41
votes
1 answer

How >> operator defines task dependencies in Airflow?

I was going through Apache Airflow tutorial https://github.com/hgrif/airflow-tutorial and encountered this section for defining task dependencies. with DAG('airflow_tutorial_v01', default_args=default_args, schedule_interval='0 * * * *', …
idazuwaika
  • 2,749
  • 7
  • 38
  • 46
39
votes
3 answers

Why does it make a difference if left and right shift are used together in one expression or not?

I have the following code: unsigned char x = 255; printf("%x\n", x); // ff unsigned char tmp = x << 7; unsigned char y = tmp >> 7; printf("%x\n", y); // 1 unsigned char z = (x << 7) >> 7; printf("%x\n", z); // ff I would have expected y and z to…
36
votes
3 answers

Should I bit-shift to divide by 2 in Java?

Possible Duplicates: Is shifting bits faster than multiplying and dividing in Java? .NET? Quick Java Optimization Question Many years ago in college, I learned that bit-shifting right by one accomplishes the same thing as dividing by two, but is…
Matt Huggins
  • 81,398
  • 36
  • 149
  • 218
35
votes
8 answers

n is negative, positive or zero? return 1, 2, or 4

I'm building a PowerPC interpreter, and it works quite well. In the Power architecture the condition register CR0 (EFLAGS on x86) is updated on almost any instruction. It is set like this. The value of CR0 is 1, if the last result was negative, 2 if…
Francesco Boffa
  • 1,402
  • 1
  • 19
  • 35