bit-manipulationbit-shiftcalculation# Is the bitwise `index>>=1` operation equal to the `index/2` and disregarding the remainder of the result?

So, I saw the following line `index >>= 1`

and I was wondering what did it mean. I did a little research and found out it was bitwise operations, but I had to simulate a few scenarios in my head and with bits that was impossible, because I had to convert everything and so on and so on, so I asked GPT about tips on how can I do it in my head. The trick I got from it was that `>>=1`

is equal to `/ 2`

and disregarding the remainder. However, I figured that, for an example, if `index = 4`

and `index = 5`

, then in both scenarios the result would be 2 which seemed odd to me.

So, I asked GPT about it and he said he did a mistake. Then I asked him which one is correct and he said the trick was correct, then said it was false and so on and so on. So, I researched online if it was true or not, yet I couldn't find it being mentioned.

So, I was wondering if anyone is more experienced and can confirm if the trick is real or was it another hallucination?

Solution

Yes, a right shift by 1 is the same as dividing by 2.

`x >> 0`

==`x / 1`

`x >> 1`

==`x / 2`

`x >> 2`

==`x / 4`

`x >> 3`

==`x / 8`

etc.

Consequently, doing *left* shifts can be used for multiplications of the same numbers:

`x << 0`

==`x * 1`

`x << 1`

==`x * 2`

`x << 2`

==`x * 4`

`x << 3`

==`x * 8`

etc.

This goes for positive integers and there are rules that must be followed. You are usually not allowed to rightshift equally many bits that the type carries or more. You are usually not allowed to leftshift so that the signbit gets set in signed integers. Some languages allows shifting negative numbers, some don't or leaves it implementation defined what happens if you do.

- Bitwise operators and signed types
- Bit-reverse a byte on 68HC12
- How can I make branchless code and how does it work?
- Setting all bits before first '1'
- Bitshift in a bitmap calculation doesn't seem to work
- Finding position of zero nibble in 64 bits
- How to do bitwise AND in javascript on variables that are longer than 32 bit?
- Function to read a specific bit from a byte and return 0 or 1
- Fastest method to define whether a number is a triangular number
- Checking register value is setting the bit using C function
- VB.NET Convert 4 bytes to an integer
- Bitwise operators and "endianness"
- Logic gate whose truth is 1101
- Mod and Division by power of two for any signed number
- How to generate lookup table for counting leading zeroes (clzlut)?
- Finding trailing 0s in a binary number
- AVX2 code to find the first longest match of 4-byte string among 8 4-byte targets
- How do I efficiently left-shift by N bits using single-bit shifts?
- Extract fractional part of double *efficiently* in C
- Reversing bits of Python integer
- Binary matrix multiplication bit twiddling hack
- How can an (int) be converted to (unsigned int) while preserving the original bit pattern?
- How to write an std::floor function from scratch
- A single byte that is from 0 to 255 to be rescaled from 0 to 7
- Converting DDL MasterFile table to SQL Server table, How do I write in missing data?
- JavaScript: convert a 52-bit integer to 20-bit and 32-bit integers
- Conversion error when accessing bits to set a bit field
- Bit manipulations
- find the bit position(s) which are exactly set twice over multiple bit fields
- How to swap nibbles in C?