设置MSB的最佳方式 [英] Best way to set MSB
问题描述
设置无符号整数类型的MSB的最佳便携方式是什么?
以下是否有任何好处?:
开始时间:0000 0000
翻转所有位:1111 1111
向右移动一次:0111 1111
翻转所有位:1000 0000
这里用代码完成:
typedef unsigned short UIntType;
UIntType msb_only =
~(〜((UIntType)0)>> 1);
是有更好的方法吗?
-Tomás
What''s the best portable way to set the MSB of an unsigned integer type?
Is the following any good?:
Start of with: 0000 0000
Flip all the bits: 1111 1111
Shift once to the right: 0111 1111
Flip all the bits: 1000 0000
Here it is done in code:
typedef unsigned short UIntType;
UIntType msb_only =
~( ~( (UIntType)0 ) >> 1 );
Is there a better way?
-Tomás
推荐答案
"Tomás" < No.Email@Address>写道:
"Tomás" <No.Email@Address> wrote:
什么是设置无符号整数类型的MSB的最佳可移植方式?
以下是否有任何好处?:
开始于:0000 0000
翻转所有位:1111 1111
向右移动一次:0111 1111
翻转所有位:1000 0000
这里是用代码完成的:
typedef unsigned short UIntType;
UIntType msb_only =
〜(〜((UIntType)0)>> ; 1);
有更好的方法吗?
What''s the best portable way to set the MSB of an unsigned integer type?
Is the following any good?:
Start of with: 0000 0000
Flip all the bits: 1111 1111
Shift once to the right: 0111 1111
Flip all the bits: 1000 0000
Here it is done in code:
typedef unsigned short UIntType;
UIntType msb_only =
~( ~( (UIntType)0 ) >> 1 );
Is there a better way?
由于在C中处理无符号整数溢出的方式,你可以
将-1转换为所需类型,替换前两步。这个
导致
UIntType msb_only =〜((UIntType)-1>> 1);
这显然更短;由您来决定是否认为它是清晰的。
Richard
Because of the way unsigned integer overflow is handled in C, you can
replace the first two steps with converting -1 to the desired type. This
results in
UIntType msb_only = ~( (UIntType)-1 >> 1 );
This is obviously shorter; up to you to decide whether you find it as
legible.
Richard
2006 -06-02,Richard Bos< rl*@hoekstra-uitgeverij.nl>写道:
On 2006-06-02, Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
"Tomás" < No.Email@Address>写道:
"Tomás" <No.Email@Address> wrote:
什么是设置无符号整数类型的MSB的最佳可移植方式?
以下是否有任何好处?:
开始于:0000 0000
翻转所有位:1111 1111
向右移动一次:0111 1111
翻转所有位:1000 0000
这里是用代码完成的:
typedef unsigned short UIntType;
UIntType msb_only =
〜(〜((UIntType)0)>> ; 1);
有更好的方法吗?
What''s the best portable way to set the MSB of an unsigned integer type?
Is the following any good?:
Start of with: 0000 0000
Flip all the bits: 1111 1111
Shift once to the right: 0111 1111
Flip all the bits: 1000 0000
Here it is done in code:
typedef unsigned short UIntType;
UIntType msb_only =
~( ~( (UIntType)0 ) >> 1 );
Is there a better way?
由于在C中处理无符号整数溢出的方式,你可以
替换将-1转换为所需类型的前两步。这个
UIntType msb_only =〜((UIntType)-1>> 1);
这显然更短;由你来决定你是否认为它清晰易读。
Because of the way unsigned integer overflow is handled in C, you can
replace the first two steps with converting -1 to the desired type. This
results in
UIntType msb_only = ~( (UIntType)-1 >> 1 );
This is obviously shorter; up to you to decide whether you find it as
legible.
我会拿一个并左移它sizeof(类型)* CHAR_BIT。
这个解决方案很容易阅读:
int m = 1<< (sizeof m * CHAR_BIT)/ *设置MSB * /
-
Andrew Poelstra< http://www.wpsoftware.net/blog >
给我发电子邮件,请使用apoelstra。在上面的地址。
你可以带领一个盲人去吸水,但是你不能让他知道它。
I would take one and left-shift it sizeof(type) * CHAR_BIT.
This solution is pretty easy to read:
int m = 1 << (sizeof m * CHAR_BIT) /* Set MSB */
--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
You can lead a blind man to water but you can''t make him chug it.
>
Andrew Poelstra写于06/02/06 11:26,:
Andrew Poelstra wrote On 06/02/06 11:26,:
2006-06-02,Richard Bos< rl *@hoekstra-uitgeverij.nl> ;写道:
On 2006-06-02, Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
"Tomás" < No.Email@Address>写道:
"Tomás" <No.Email@Address> wrote:
设置无符号整数类型的MSB的最佳可移植方式是什么?
以下是否有任何好处? :
开始于:0000 0000
翻转所有位:1111 1111
向右移动一次:0111 1111
翻转所有位:1000 0000
这里用代码完成:
typedef unsigned short UIntType;
UIntType msb_only =
〜(〜((UIntType)0 )>>> 1);
有更好的方法吗?
What''s the best portable way to set the MSB of an unsigned integer type?
Is the following any good?:
Start of with: 0000 0000
Flip all the bits: 1111 1111
Shift once to the right: 0111 1111
Flip all the bits: 1000 0000
Here it is done in code:
typedef unsigned short UIntType;
UIntType msb_only =
~( ~( (UIntType)0 ) >> 1 );
Is there a better way?
由于在C中处理无符号整数溢出的方式,你可以
将-1转换为所需类型,替换前两步。这个
UIntType msb_only =〜((UIntType)-1>> 1);
这显然更短;由你来决定你是否觉得它清晰可辨。
Because of the way unsigned integer overflow is handled in C, you can
replace the first two steps with converting -1 to the desired type. This
results in
UIntType msb_only = ~( (UIntType)-1 >> 1 );
This is obviously shorter; up to you to decide whether you find it as
legible.
我会拿一个并左移它sizeof(类型)* CHAR_BIT。
int m = 1<< (sizeof m * CHAR_BIT)/ *设置MSB * /
I would take one and left-shift it sizeof(type) * CHAR_BIT.
This solution is pretty easy to read:
int m = 1 << (sizeof m * CHAR_BIT) /* Set MSB */
这是错误的。 R-O-N-G,错了。从哪里开始?
- 它假设int的所有位都是值位,而
忽略了填充位的可能性。好吧,
可能更像是一个理论的而不是实际
问题,但它不是唯一的...
- 移位运算符仅在移位时定义
距离严格小于
移位值的宽度。缺少一个-1,没有它,上面会产生未定义的行为(6.5.7 / 3)。在
实际机器上,可能的结果是m = 0或m = 1。
- 即使是`-1' ',以上是尝试将
a值位移到符号位置,再一次
产生未定义的行为(6.5.7 / 4)。缺少的'-1''
应该是'-2'',这取决于你如何选择
来定义有符号整数的MSB 。
- 说到签名整数,OP特别是
询问* un * signed整数。
如果有人为你提供这个解决方案,我会建议您不要喝它,而不是
。
-
Er ********* @ sun.com
This is wrong. R-O-N-G, wrong. Where to begin?
- It assumes all bits of an `int'' are value bits, and
ignores the possibility of padding bits. All right,
that may be more of a "theoretical" than an "actual"
problem, but it''s not the only one ...
- Shift operators are only defined when the shift
distance is strictly less than the width of the
shifted value. There''s a `-1'' missing, without which
the above yields undefined behavior (6.5.7/3). On
actual machines, the likely result is `m=0'' or `m=1''.
- Even with the `-1'', the above is an attempt to shift
a value bit into the sign position, which once again
yields undefined behavior (6.5.7/4). The missing `-1''
should perhaps be a `-2'', depending on how you choose
to define the "M"SB of a signed integer.
- Speaking of signed integers, the O.P. specifically
asked about *un*signed integers.
If somebody offers you this "solution," I''d recommend
that you not drink it.
--
Er*********@sun.com
这篇关于设置MSB的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!