位掩码 [英] Bit mask

查看:77
本文介绍了位掩码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是一种有效的方法来掩盖8位字符的最后3位并使它们

全部为零?


位 - 转移是可能的,但不确定它是否足够有效。


示例:


01011 [010] - > 01011 [000]


谢谢!

What''s an efficient way to mask a last 3 bits of a 8-bit char and make them
all zero?

Bit-shifting is possible but not sure if it is efficient enough.

Example:

01011[010] --> 01011[000]

Thanks!

推荐答案

Als< no **** @ nowhere。净>潦草地写道:
Als <no****@nowhere.net> scribbled the following:
什么是一种有效的方法来掩盖8位字符的最后3位并使它们全部为零?
可以进行位移,但不确定它是否足够有效。
示例:
01011 [010] - > 01011 [000]
谢谢!
What''s an efficient way to mask a last 3 bits of a 8-bit char and make them
all zero? Bit-shifting is possible but not sure if it is efficient enough. Example: 01011[010] --> 01011[000] Thanks!




好​​吧,一个非常简单的方法是和〜7进行AND运算。

例如:

unsigned char x = 0x5A; / *与二进制01011010相同* /

x = x& 〜7; / *清除最后3位* /

效率高吗?这取决于你的实施。


-

/ - Joona Palaste(pa*****@cc.helsinki.fi)--- ----------芬兰-------- \

\-- http://www.helsinki.fi/~palaste --------------------- rules ! -------- /

C ++看起来像线路噪音。

- Fred L. Baube III



Well, a really simple way would be ANDing with ~7.
For example:
unsigned char x = 0x5A; /* the same as binary 01011010 */
x = x & ~7; /* clear last 3 bits */
Is it efficient? It depends on your implementation.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"C++ looks like line noise."
- Fred L. Baube III


Mark A. Odell写道:
Mark A. Odell wrote:
Joona I Palaste< pa ***** @ cc.helsinki.fi>在
新闻中写道:bt ********** @ oravannahka.helsinki.fi:
Joona I Palaste <pa*****@cc.helsinki.fi> wrote in
news:bt**********@oravannahka.helsinki.fi:
unsigned char x = 0x5A;
unsigned char x = 0x5A;


x& =(unsigned char)~0x07;
x &= (unsigned char) ~0x07;



这几乎是一回事,无论效率差异如何,编译器优化都可能会丢失。我只是认为我的版本更清晰易读。适合自己。



It''s practically the same thing, and whatever efficiency difference
there would be will probably get lost in compiler optimisation. I just
thought my version was clearer to read. Suit yourself.



我的意思是没有WRT效率,只是因为我认为& =是更常见的习惯用法,你需要将~0x07转换为unsigned char。


I meant nothing WRT efficiency, just that I thought that &= is the more
common idiom and that you needed to cast ~0x07 to unsigned char.




为什么需要演员?这对我来说似乎毫无用处,因为结果

将被提升回int(无论如何,可能是unsigned int理论上也是

)。

-Kevin

-

我的电子邮件地址有效,但会定期更改。

要联系我,请使用地址来自最近的帖子。



Why would the cast be needed? It seems useless to me, since the result
will be promoted back to int (probably - unsigned int is theoretically
possible also) anyway.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.


Eric Sosman< Er ********* @ sun.com>在消息中写道

新闻:3F *************** @sun.com ...
Eric Sosman <Er*********@sun.com> wrote in message
news:3F***************@sun.com...
Als写道:

什么是掩盖8位字符的最后3位并使
全部为零的有效方法?

位移是可能但不确定它是否足够有效。

示例:

01011 [010] - > 01011 [000]

What''s an efficient way to mask a last 3 bits of a 8-bit char and make them all zero?

Bit-shifting is possible but not sure if it is efficient enough.

Example:

01011[010] --> 01011[000]



许多甚至大多数C实现都使用了一个8位`char'',但实际上并没有保证语言的语言并且已知存在使用更宽的`char''
的实现。仍然:

unsigned char byte = 0x5A; / * 00 ... 01011010 * /



Many or perhaps even most C implementations use an
eight-bit `char'', but that is not actually guaranteed
by the language, and implementations using wider `char''
are known to exist. Still:

unsigned char byte = 0x5A; /* 00...01011010 */




您是否有任何理由使用unsigned char?而不是char以上?

谢谢!



Is there any reason that you use "unsigned char" instead of "char" above?
Thanks!


这篇关于位掩码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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