views:

1434

answers:

3

This is in relation to a homework assignment but this is not the homework assignment.

I'm having difficultly understanding if there is a difference on how the bitwise not (~ in C) would affected signed int and unsigned int when compiled on a big endian machine vs. a little endian machine.

Are the bytes really "backwards" and if so does the bitwise not (and other operators) cause different resulting ints be produced depending on the machine type?

While we are at it, is the answer the same for each of the bitwise operators in C or does it heavily depend?

The operators I'm referring to are:

~  /* bitwise Not */
&  /* bitwise And */
|  /* bitwise Or */
^  /* bitwise Exclusive-Or */

Thank you in Advance!

Regards,
Frank

Update: In reading my responses thus far, I feel compelled to ask if the bitwise not operator affects the sign bit on a signed int. I'm afraid I've been a bit confused on this part as I forgot about all that stillyness. Adam seems to be stating that all values are treated as unsigned. Is the sign-bit reapplied or does the once signed value become unsigned?

+4  A: 

The bitwise operators and logical operators all operate identically in big- and little-endian machines. Likewise, they also operator identically on signed and unsigned data: the result is as if everything were unsigned. So, if x and y are signed integers, then ~x equals (int)(~((unsigned int)x) and x & y equals (int)(((unsigned int)x) & ((unsigned int)y), and so on for all of the other operators.

Adam Rosenfield
Right, endianness doesn't matter until you start addressing the individual bytes of an integer via pointer arithmetic/addressing games. Also FYI the listed operators aren't affected by signedness, but the right shift operator `>>` can be.
John Kugelman
So, ~54321 will always result in 703710 ?
Frank V
Thank you very, very much.
Frank V
No, the bits will be the same but the numerical values will be interpreted differently. With signed two's-complement integers, ~n = -n-1. ~4321 = -4322
John Kugelman
+1  A: 

As @Adam's answer says, since these operators go bit-by-bit, byte orders doesn't really matter! For example, (~x) == (-x-1) for every signed integer x, no matter the length of x in bytes, AND no matter the machine's endianness (as long as it uses two's-complement arithmetic -- are there ANY left that don't?-)

Alex Martelli
You are agreeing with Adam, right?
Frank V
Yes, I'm pointing out that the dependency is on two's-complement arithmetic (which to the best of my knowledge has been just about universal in CPUs for a [human;-)] generation or more) rather than on endianness.
Alex Martelli
+1  A: 

The logical operators just care about if their arguments are zero or not, they don't care how these values are actually represented in memory. So for the logical operations endianness doesn't matter.

The bitwise operators always act on all bits of their arguments and they have an effect on the different bit positions individually (bit #5 in the result just depends on bits #5 in the inputs). They don't care about the order in which the bits are stored or if some of the bits might have special meaning for the data type, they just process all of them. So here endianness also doesn't matter because simply all bits are affected, no matter their order.

sth