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
22
votes
7 answers

Bitshifting a long in Java

Im sure this is an easy one for whoever sees it first! Why in Java does code like long one = 1 << 0; long thirty = 1 << 30; long thirtyOne = 1 << 31; long thirtyTwo = 1 << 32; System.out.println(one+" = "+Long.toBinaryString(1 <<…
Dori
  • 18,283
  • 17
  • 74
  • 116
21
votes
3 answers

shift bits vs multiply in PHP

I have the following code:
aki
  • 1,241
  • 2
  • 13
  • 43
21
votes
7 answers

Optimization of C code

For an assignment of a course called High Performance Computing, I required to optimize the following code fragment: int foobar(int a, int b, int N) { int i, j, k, x, y; x = 0; y = 0; k = 256; for (i = 0; i <= N; i++) { …
franvergara66
  • 10,524
  • 20
  • 59
  • 101
21
votes
4 answers

In C++, what is the difference between 1 and 1i64?

I'm converting some 32-bit compatible code into 64-bit - and I've hit a snag. I'm compiling a VS2008 x64 project, and I receive this warning: warning C4334: '<<' : result of 32-bit shift implicitly converted to 64 bits (was 64-bit shift…
Nick Bolton
  • 38,276
  • 70
  • 174
  • 242
21
votes
8 answers

How to extract specific bits from a number in C?

I need to extract specific part (no of bits) of a short data type in C. For Example I have a binary of 52504 as 11001101000 11000 and I want First 6 ( FROM LSB --> MSB i.e 011000 decimal 24) bits and rest of 10 bits ( 11001101000 decimal…
Usman
  • 2,742
  • 4
  • 44
  • 82
20
votes
2 answers

Shift count negative or too big error - correct solution?

I have the following function for reading a big-endian quadword (in a abstract base file I/O class): unsigned long long File::readBigEndQuadWord(){ unsigned long long qT = 0; qT |= readb() << 56; qT |= readb() << 48; qT |= readb() << 40; …
PeterK
  • 6,287
  • 5
  • 50
  • 86
19
votes
7 answers

Left bit shifting 255 (as a byte)

Can anyone explain why the following doesn't compile? byte b = 255 << 1 The error: Constant value '510' cannot be converted to a 'byte' I'm expecting the following in binary: 1111 1110 The type conversion has stumped me.
Chris S
  • 64,770
  • 52
  • 221
  • 239
19
votes
2 answers

What does shift left (<<) actually do in Swift?

I'm messing around with a Flappy Bird clone, and i can't figure out what the meaning of the following code is let birdCategory: UInt32 = 1 << 0 let worldCategory: UInt32 = 1 << 1 let pipeCategory: UInt32 = 1 << 2 let scoreCategory: UInt32 = 1 <<…
Graham
  • 217
  • 1
  • 2
  • 5
18
votes
3 answers

Does a C shift expression have unsigned type? Why would Splint warn about a right-shift?

For the following program: int main(void) { int value = 2; int result = value >> 1U; return result; } ...Splint 3.1.2 gives the warning: splint_test.c: (in function main) splint_test.c:4:18: Variable result initialized to type unsigned…
detly
  • 29,332
  • 18
  • 93
  • 152
18
votes
3 answers

What is the difference between directly assigning the result of left shift operation to a variable and the left shift assignment operation in C?

In the following expression, the result of the left shift operation is assigned to the variable i. int i; i = 7 << 32; printf("i = %d\n",i); In the following expression, the left shift assignment operation is carried. int x = 7; x <<= 32; printf("x…
user5155804
18
votes
2 answers

Bit Twiddling Hacks: interleave bits the obvious way

i am interested on this problem Interleave bits the obvious way (from http://graphics.stanford.edu/~seander/bithacks.html) unsigned short x; // Interleave bits of x and y, so that all of the unsigned short y; // bits of x are in the even…
user466534
18
votes
3 answers

Bit Setting and Bit Shifting in Ansi C

Can anyone explain this bitwise operation syntax? #define Bitset(var,bitno) ((var) |=1UL<<(bitno)) I know it sets the bits of var, but I can't understand the syntax.
tcop
  • 381
  • 1
  • 2
  • 11
17
votes
5 answers

Getting upper and lower byte of an integer in C# and putting it as a char array to send to a com port, how?

In C I would do this int number = 3510; char upper = number >> 8; char lower = number && 8; SendByte(upper); SendByte(lower); Where upper and lower would both = 54 In C# I am doing this: int number = Convert.ToInt16("3510"); …
rollsch
  • 2,518
  • 4
  • 39
  • 65
17
votes
3 answers

Difference between >> and >>> in Scala

Is there any difference between >> and >>> operator in Scala? scala> 0x7f >>> 1 res10: Int = 63 scala> 0x7f >> 1 res11: Int = 63 scala> 0x7f >> 4 res12: Int = 7 scala> 0x7f >>> 4 res13: Int = 7
Kokizzu
  • 24,974
  • 37
  • 137
  • 233