Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
453 views
in Technique[技术] by (71.8m points)

undefined behavior - What is the definition of bitwise operators in C++?

According to the standard, operator << yields an Undefined Behavior for a negative signed first operand.

C++11 5.8.2

The value of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are zero-
filled. If E1 has an unsigned type, the value of the result is E1 × 2 pow E2,
reduced modulo one more than the maximum value representable in the result type.
Otherwise, if E1 has a signed type and non-negative value, and E1 × 2 pow E2 is
representable in the result type, then that is the resulting value; otherwise,
the behavior is undefined

This is understandable as the layout of integers in memory is implementation defined.

C++11 3.9.1.7

this International Standard permits 2’s complement, 1’s complement and
signed magnitude representations for integral types.

On the other hand, the standard do not seem to define exactly what bitwise & | and ^ should do.

C++11 5.11 Bitwise AND operator

and-expression:
    equality-expression
    and-expression & equality-expression
1 The usual arithmetic conversions are performed; the result is the bitwise 
AND function of the operands. The operator applies only to integral
or unscoped enumeration operands.

C++11 5.12 Bitwise exclusive OR operator

exclusive-or-expression:
    and-expression
    exclusive-or-expression ? and-expression
1 The usual arithmetic conversions are performed; the result is the bitwise
exclusive OR function of the operands. The operator applies only to integral
or unscoped enumeration operands.

C++11 5.13 Bitwise inclusive OR operator

inclusive-or-expression:
    exclusive-or-expression
    inclusive-or-expression | exclusive-or-expression
1 The usual arithmetic conversions are performed; the result is the bitwise
inclusive OR function of its operands. The operator applies only to integral
or unscoped enumeration operands.

The definition of those operators completely eludes me. Is it somewhere else in the standard ? Is the result implementation defined for signed integer ?

As an example let's look at this code:

signed char a=-1;
signed char b=3;
signed char c=a&b;

With 2's complement, a is 1111 1111, and b is 0000 0011. Finally c equals 0000 0011 (+3).

With 1's complement, a is 1111 1110, and b is 0000 0011. Does c equals 0000 0010 (+2) ?

With sign-magnitude, a is 1000 0001, and b is 0000 0011. Does c equals 0000 0001 (+1) ?

If you have access to platforms using 1's complement or sign-magnitude, what is the result on those platforms ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The bitwise operations operate on each bit independently, whatever each bit might happen to mean when interpreted as part of a numeric type.

So yes, 10000001 & 00000011 == 00000001, regardless of whether each bit represents a sign or part of a value.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...