C ++中按位运算符的定义是什么? [英] What is the definition of bitwise operators in C++?

查看:183
本文介绍了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按位与运算符

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按位异或运算符

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按位包含或运算符

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;

用2的补码表示,a是1111 1111,b是00000011.最后c等于0000 0011(+3).

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

用1的补码表示,a是1111 1110,b是00000011.c是否等于0000 0010(+2)?

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

在具有符号幅度的情况下,a为1000 0001,b为00000011.c是否等于0000 0001(+1)?

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

如果您可以使用1的补码或符号幅度访问平台,那么在这些平台上会有什么结果?

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

推荐答案

按位运算独立地对每个位进行操作,而当将每个位解释为数字类型的一部分时,可能碰巧意味着什么.

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

是的,10000001 & 00000011 == 00000001,无论每个位代表符号还是值的一部分.

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

这篇关于C ++中按位运算符的定义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆