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

What does >> do in Java?

Okay, I tried looking up what >>, or shift means, but it's way over my head as this site explains it: http://www.janeg.ca/scjp/oper/shift.html What would the explanation be, if talking to a kid?
William
  • 8,630
  • 23
  • 77
  • 110
26
votes
6 answers

How does 1 << 64 - 1 work?

At http://tour.golang.org/#14 they show an example where the number 1 is shifted by 64 bits. This of course would result in an overflow, but then it is subtracted by 1 and all is well. How does half of the expression result in a failure while the…
Parris
  • 17,833
  • 17
  • 90
  • 133
25
votes
8 answers

Shifting a Java BitSet

I am using a java.util.BitSet to store a dense vector of bits. I want to implement an operation that shifts the bits right by 1, analogous to >>> on ints. Is there a library function that shifts BitSets? If not, is there a better way than the…
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
25
votes
3 answers

Is 1 << 31 well defined in C when sizeof(int) == 4

According to the answer to this questions: The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are filled with zeros. If E1 has an unsigned type, the value of the result is E1 × 2E2, reduced modulo one more than the maximum…
Ilya Lesokhin
  • 359
  • 2
  • 7
25
votes
6 answers

Behaviour of unsigned right shift applied to byte variable

Consider the following snip of java code byte b=(byte) 0xf1; byte c=(byte)(b>>4); byte d=(byte) (b>>>4); output: c=0xff d=0xff expected output: c=0x0f how? as b in binary 1111 0001 after unsigned right shift 0000 1111 hence 0x0f but why is it…
Radheshyam Nayak
  • 1,038
  • 3
  • 11
  • 20
24
votes
3 answers

Google Protocol Buffers: ZigZag Encoding

From "Signed Types" on Encoding - Protocol Buffers - Google Code: ZigZag encoding maps signed integers to unsigned integers so that numbers with a small absolute value (for instance, -1) have a small varint encoded value too. It does this in a way…
Thanatos
  • 42,585
  • 14
  • 91
  • 146
24
votes
3 answers

How is shift operator evaluated in C?

I recently noticed a (weird) behavior when I conducted operations using shift >> <
chouaib
  • 2,763
  • 5
  • 20
  • 35
24
votes
4 answers

Is BitArray faster in C# for getting a bit value than a simple conjuction with bitwise shift?

1). var bitValue = (byteValue & (1 << bitNumber)) != 0; 2). using System.Collections.BitArray with a Get(int index) method What is faster? In what situations for the .NET projects BitArray could be more useful than a simple conjunction with the…
Secret
  • 2,627
  • 7
  • 32
  • 46
24
votes
5 answers

Converting from RGB ints to Hex

What I have is R:255 G:181 B:178, and I am working in C# (for WP8, to be more specific) I would like to convert this to a hex number to use as a color (to set the pixel color of a WriteableBitmap). What I am doing is the following: int hex = (255 <<…
Toadums
  • 2,772
  • 8
  • 44
  • 67
23
votes
6 answers

Why do we need to use shift operators in java?

What is the purpose of using Shift operators rather than using division and multiplication? Are there any other benefits of using shift operators? Where should one try to use the shift operator?
Saravanan
  • 11,372
  • 43
  • 143
  • 213
23
votes
2 answers

Bitwise shift operators. Signed and unsigned

I'm practising for the SCJP exam using cram notes from the Internet. According to my notes the >> operator is supposed to be signed right shift, with the sign bit being brought in from the left. While the left shift operator << is supposed to…
user271052
22
votes
6 answers

Will bit-shift by zero bits work correctly?

Say I have a function like this: inline int shift( int what, int bitCount ) { return what >> bitCount; } It will be called from different sites each time bitCount will be non-negative and within the number of bits in int. I'm particularly…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
22
votes
2 answers

What is the result type of the bit shift operator?

Consider the following listing: #include #include static_assert(std::is_same_v); static_assert(std::is_same_v); static_assert(std::is_same_v
ivaigult
  • 6,198
  • 5
  • 38
  • 66
22
votes
1 answer

Meaning of <<= and |=

What is the meaning of <<= and |= in C? I recognise << is bitshift etc. but I don't know what these are in combination.
Ken
  • 30,811
  • 34
  • 116
  • 155
22
votes
7 answers

Weird behavior of right shift operator (1 >> 32)

I recently faced a strange behavior using the right-shift operator. The following program: #include #include #include #include int foo(int a, int b) { return a >> b; } int bar(uint64_t a, int b) { …
ereOn
  • 53,676
  • 39
  • 161
  • 238