Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does bit-shift depend on endianness?

Tags:

c

endianness

Suppose I have the number 'numb'=1025 [00000000 00000000 00000100 00000001] represented:

On Little-Endian Machine:

00000001 00000100 00000000 00000000

On Big-Endian Machine:

00000000 00000000 00000100 00000001

Now, if I apply Left Shift on 10 bits (i.e.: numb <<= 10), I should have:

[A] On Little-Endian Machine:

As I noticed in GDB, Little Endian does the Left Shift in 3 steps: [I have shown '3' Steps to better understand the processing only]

  1. Treat the no. in Big-Endian Convention:

    00000000        00000000        00000100    00000001
    
  2. Apply Left-Shift:

    00000000        00010000        00000100        00000000
    
  3. Represent the Result again in Little-Endian:

    00000000        00000100        00010000        00000000 
    

[B]. On Big-Endian Machine:

00000000        00010000        00000100        00000000

My Question is:

If I directly apply a Left Shift on the Little Endian Convention, it should give:

numb:

00000001 00000100 00000000 00000000

numb << 10:

00010000 00000000 00000000 00000000

But actually, it gives:

00000000        00000100        00010000        00000000 

To achieve the second result only, I have shown three hypothetical steps above.

Please explain me why the above two results are different: The actual outcome of numb << 10 is different than the expected outcome.

like image 946
Sandeep Singh Avatar asked Oct 16 '22 00:10

Sandeep Singh


People also ask

Does endianness affect Bitwise operations?

So no, it doesn't matter in bitwise operations.

What does endianness depend on?

Broadly speaking, the endianness in use is determined by the CPU. Because there are a number of options, it is unsurprising that different semiconductor vendors have chosen different endianness for their CPUs.

What is the significance of endianness?

Endianness is a term that describes the order in which a sequence of bytes is stored in computer memory. Endianness can be either big or small, with the adjectives referring to which value is stored first.

Does endianness matter for a single byte?

Again, endian-ness does not matter if you have a single byte. If you have one byte, it's the only data you read so there's only one way to interpret it (again, because computers agree on what a byte is). Now suppose we have our 4 bytes (W X Y Z) stored the same way on a big-and little-endian machine.


1 Answers

Endianness is the way values are stored in memory. When loaded into the processor, regardless of endianness, the bit shift instruction is operating on the value in the processor's register. Therefore, loading from memory to processor is the equivalent of converting to big endian, the shifting operation comes next and then the new value is stored back in memory, which is where the little endian byte order comes into effect again.

Update, thanks to @jww: On PowerPC the vector shifts and rotates are endian sensitive. You can have a value in a vector register and a shift will produce different results on little-endian and big-endian.

like image 217
Carl Avatar answered Oct 17 '22 13:10

Carl