Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bitwise operators and signed types

I'm reading C++ Primer and I'm slightly confused by a few comments which talk about how Bitwise operators deal with signed types. I'll quote:

Quote #1

(When talking about Bitwise operators) "If the operand is signed and its value is negative, then the way that the “sign bit” is handled in a number of the bitwise operations is machine dependent. Moreover, doing a left shift that changes the value of the sign bit is undefined"

Quote #2

(When talking about the rightshift operator) "If that operand is unsigned, then the operator inserts 0-valued bits on the left; if it is a signed type, the result is implementation defined—either copies of the sign bit or 0-valued bits are inserted on the left."

The bitwise operators promote small integers (such as char) to signed ints. Isn't there an issue with this promotion to signed ints when bitwise operators often gives undefined or implementation-defined behaviour on signed operator types? Why wouldn't the standard promote char to unsigned int?


Edit: Here is the question I took out, but I've placed it back for context with some answers below.

An exercise later asks

"What is the value of ~'q' << 6 on a machine with 32-bit ints and 8 bit chars, that uses Latin-1 character set in which 'q' has the bit pattern 01110001?"

Well, 'q' is a character literal and would be promoted to int, giving

~'q' == ~0000000 00000000 00000000 01110001 == 11111111 11111111 11111111 10001110

The next step is to apply a left shift operator to the bits above, but as quote #1 mentions

"doing a left shift that changes the value of the sign bit is undefined"

well I don't exactly know which bit is the sign bit, but surely the answer is undefined?

like image 255
Silversonic Avatar asked Jan 25 '15 21:01

Silversonic


1 Answers

You're quite correct -- the expression ~'q' << 6 is undefined behavior according to the standard. Its even worse than you state, as the ~ operator is defined as computing "The one's complement" of the value, which is meaningless for a signed (2s-complement) integer -- the term "one's complement" only really means anything for an unsigned integer.

When doing bitwise operations, if you want strictly well-defined (according to the standard) results, you generally have to ensure that the values being operated on are unsigned. You can do that either with explicit casts, or by using explicitly unsigned constants (U-suffix) in binary operations. Doing a binary operation with a signed and unsigned int is done as unsigned (the signed value is converted to unsigned).

C and C++ are subtley different with the integer promotions, so you need to be careful here -- C++ will convert a smaller-than-int unsigned value to int (signed) before comparing with the other operand to see what should be done, while C will compare operands first.

like image 95
Chris Dodd Avatar answered Sep 28 '22 03:09

Chris Dodd