标准(跨平台)的位操作方式 [英] Standard (cross-platform) way for bit manipulation

查看:263
本文介绍了标准(跨平台)的位操作方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于是有数字的不同的二进制重新presentation(例如,以大/小端),这是跨平台的:

As are are different binary representation of the numbers (for example, take big/little endian), is this cross-platform:

// NOTE: FIXED-SIZE unsigned integral type
some_unsigned_type variable = some_number;

// set n-th bit, starting from 1,
// right-to-left (least significant-to most significant)
variable |= ( 1 << ( n - 1 ) );

// clear the same bit:    
variable &= ~( 1 << ( n - 1 ) );

在换句话说,没有编译器总是照顾的固定大小的不同的二进制重新presentation的无符号数,或者是特定于平台的?

In other words, does the compiler always take care of the different binary representation of the fixed size unsigned numbers, or it's platform-specific?

而如果变量签署整数类型(例如, INT ),其值为

And what if variable is signed integral type (for example, int) and its value is




  • 负?

什么的的标准的说一下吗?

<子> P.S。而且,,我是既有趣 - C C ++ ,请不要'T告诉我,他们有不同的语言,因为我知道这一点:)

P.S. And, yes, I'm interesting in both - C and C++, please don't tell me they are different languages, because I know this :)

<子>我可以粘贴真实的例子,如果需要的话,但后期会变得太长

推荐答案

声明:我隐含假设你是在谈论一个整数类型有固定的宽度。位移,否则是很危险的......

Disclaimer: I am implicitly assuming that you are talking about an integer type with a fixed width. Bit-shifting otherwise is quite hazardous...

标准:n3337 C ++ 11

移位的定义是数学在签名类型无符号类型或正值(*),因此,不会受到底层硬件重新presentation

The definition of shifts is mathematical for unsigned types or positive values in signed types (*), and therefore not affected by the underlying hardware representation.

5.8移位运算[expr.shift]

2 E1 1所述的价值;&LT; E2 E1 左移 E2 位的位置;腾空位零填充。如果 E1 有一个无符号类型,则结果的值为 E1×2 E2 ,模数减比结果类型的最大值再presentable一个。否则,如果 E1 有符号类型和非负值, E1×2 E2 在结果类型重新presentable,然后就是得到的值;否则,行为是不确定的。

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 × 2E2, 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×2E2 is representable in the result type, then that is the resulting value; otherwise, the behavior is undefined.

3 E1&gt;中值;&GT; E2 E1 右移 E2 位的位置。如果 E1 有一个无符号的类型,或者 E1 有符号类型和非负值,值了结果是商的组成部分 E1 / 2 E2 。如果 E1 有签署类型和负值,结果值是实现定义的。

3 The value of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an unsigned type or if E1 has a signed type and a non-negative value, the value of the result is the integral part of the quotient of E1/2E2. If E1 has a signed type and a negative value, the resulting value is implementation-defined.

由于同样的原因,我想按位否定是可以的:他们是数学上定义

For the same reason, I would think the bitwise and, or and negate are okay: they are defined mathematically.

5.3.1一元运算符[expr.unary.op]

10 的操作应具有完整的或无作用域的枚举类型;其结果是它的操作数的补数。

10 The operand of ˜ shall have integral or unscoped enumeration type; the result is the one’s complement of its operand.

5.11位与运算符[expr.bit.and]

1 的通常的算术转换执行;结果是操作数的按位与功能。操作仅适用于整体或无作用域枚举操作数。

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.

5.13按位或运算符[expr.or]

1 的通常的算术转换执行;结果是操作数的按位或功能。操作仅适用于整体或无作用域枚举操作数。

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.

不过我承认我对后两个不太确定,我找不到任何定义的按位XX功能的,所以尽管我相信他们是指他们的数学同行,我可以提供无保证

However I will admit I am less sure for the latter two, I could not find any definition of bitwise XX function, so even though I believe they refer to they mathematical counterparts I can offer no assurance.

(*)由于phresnel指出了这一点。

(*) Thanks to phresnel for pointing that out.

这篇关于标准(跨平台)的位操作方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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