views:

1110

answers:

4

Does Endianness matter at all with the bitwise operations. Either logical or shifting?

I'm working on homework with regard to bitwise operators and I can not make heads or tails on it and I think I'm getting quite hung up on the endianess. That is, I'm using a little endian machine (like most are) but does this need to be considered or is it a wasted fact?

In case it matters, I'm using C.

Thank you,
Frank

+1  A: 

You haven't specified a language but usually, programming languages such as C abstract endianness away in bitwise operations. So no, it doesn't matter in bitwise operations.

Mehrdad Afshari
Given that the question has no revisions, I'm surprised you say he hasn't mentioned language, when he does, and it's also tagged as C.
Simeon Pilgrim
@Simeon: It didn't have at the time I wrote this answer. Edits by a single author in a small time frame will be merged into one. That's why you're seeing it as a single revision.
Mehrdad Afshari
+13  A: 

Endianness only matters for layout of data in memory. As soon as data is loaded by the processor to be operated on, endianness is completely irrelevent. Shifts, bitwise operations, and so on perform as you would expect (data logically laid out as low-order bit to high) regardless of endianness.

Michael
+9  A: 

The bitwise operators abstract away the endianness. For example, the >> operator always shifts the bits towards the least significant digit. However, this doesn't mean you are safe to completely ignore endianness when using them, for example when dealing with individual bytes in a larger structure you cannot always assume that they will fall in the same place.

short temp = 0x1234;
temp >> 8;

// on little endian, c will be 12, on big endian, it will be 0
char c=((char*)temp)[0];

To clarify, I am not in basic disagreement with the other answers here. The point I am trying to make is to emphasise that although the bitwise operators are essentially endian neutral, you cannot ignore the effect of endianess in your code, especially when combined with other operators.

1800 INFORMATION
You are basically disagreeing with everyone but yet your answer was voted the best. How can identify the behaviors?
Frank V
I have added some further clarification
1800 INFORMATION
+1  A: 

As others have mentioned, shifts are defined by the C language specification and are independent of endianness, but the implementation of a right shift may vary depending on iff the architecture uses one's complement or two's complement arithmetic.

rpetrich