面具:为什么? [英] Masks: Why?

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

问题描述

我经常看到C源代码中使用的掩码。我真的不会理解这个目的或功能。我见过用过:


#define MASK 0x77

....


if (你好&MASK)


此外,我(不是仍然不理解)按位

运营商的目的。我知道有时你可以转到

函数:


func(ONE | TWO | THREE);


通过这三个选项。我不明白的是

是什么意思,它在做什么,以及该功能如何知道你通过了

一,二,三而不是一些价值等于ONE |两个|三美元b $ b和另一件事是如果ONE |三= =两个|四。那么呢?

主要是我不明白为什么要使用面具,或者为什么会使用

位运算符。我很确定我明白了什么是一个

按位运算符我只是不知道你为什么需要那个

完成。感谢先进的任何人,他们帮助那些彻底感到困惑的人。

Nori

I have often times seen masks used in C source code. I really don''t
understand the purpose or functionality of this. I have seen used:

#define MASK 0x77

....

if(hello & MASK)

Also I don''t (yes still don''t) understand the purpose of the bitwise
operators. I understand there are times when you may pass to a
function:

func(ONE | TWO | THREE);

To pass those three options. What I don''t understand is what that
means, what it is doing, and how the function knows that you passed
ONE, TWO, and THREE and not some value just equal to ONE | TWO | THREE
and another thing is what if ONE | THREE == TWO | FOUR. What then?
Mainly I just don''t understand WHY a mask would be used, or why a
bitiwise operator would be used. I''m pretty sure I understand what a
bitwise operator does I just don''t know why you would EVER need that
done. Thanks in advanced to anyone who helps someone who is thoroughly
confused.
Nori

推荐答案


no*********@gmail.com 写道:
我经常看到C源代码中使用的掩码。我真的不明白这个的目的或功能。我见过用过:

#define MASK 0x77


如果(你好&MASK)

>此外,我(不是仍然不)理解按位运算符的目的。我知道有些时候你可能会转到
函数:

func(ONE | TWO | THREE);

要通过这三个选项。我不明白的是,
意味着什么,它在做什么,以及该功能如何知道你通过了一,二和三而不是一些值等于一个|两个|如果ONE |,那么另外一件事就是如此三= =两个|四。然后呢?
主要是我不明白为什么会使用面具,或者为什么会使用
bitiwise操作符。我很确定我明白了什么是一个按位运算符,我只是不知道为什么你需要完成它。非常感谢任何帮助完全困惑的人。
I have often times seen masks used in C source code. I really don''t
understand the purpose or functionality of this. I have seen used:

#define MASK 0x77

...

if(hello & MASK)

Also I don''t (yes still don''t) understand the purpose of the bitwise
operators. I understand there are times when you may pass to a
function:

func(ONE | TWO | THREE);

To pass those three options. What I don''t understand is what that
means, what it is doing, and how the function knows that you passed
ONE, TWO, and THREE and not some value just equal to ONE | TWO | THREE
and another thing is what if ONE | THREE == TWO | FOUR. What then?
Mainly I just don''t understand WHY a mask would be used, or why a
bitiwise operator would be used. I''m pretty sure I understand what a
bitwise operator does I just don''t know why you would EVER need that
done. Thanks in advanced to anyone who helps someone who is thoroughly
confused.




我仍​​然会说你没有很好地掌握表示

值。我建议你回到一些涵盖它的教科书。


按位运算符和掩码用于获取/设置/测试单个位

in a multi比特值。大多数情况下,这些位代表各种标志

(例如,如果设置了第5位,您的MP3播放器将处于重复模式)。其他

次,多个值被打包到同一个多位字段中。好的

示例是浮点数的存储格式。很多时候(而且我可能会过度简化),它们将被打包到一个字段中,而不是像这样:
不同于:尾数符号为1位,N为值
尾数
,指数符号为1位,指数值为

值为M位。


希望这能澄清一些事情。



I''d still say you don''t have a good grasp on bit representation of
values. I suggest you go back to some textbooks covering that.

Bitwise operators, and masks, are used to get/set/test individual bits
in a multi-bit value. Most often, these bits represent various flags
(e.g., if bit 5 is set, your MP3 player will be in repeat mode). Other
times, several values are packed into the same multi-bit field. A good
example is storage format of floating point numbers. Quite often (and I
am probably oversimplifying), they will be packed into a field not
unlike this: 1 bit for the sign of the mantissa, N bits for the value
of the mantissa, 1 bit for the sign of the exponent, M bits for the
value of the exponent.

Hope this clarifies things a bit.


no*********@gmail.com 写道:
no*********@gmail.com wrote:
我经常看到C源代码中使用的掩码。我真的不明白这个的目的或功能。我见过用过:

#define MASK 0x77


如果(你好&MASK)

>此外,我(不是仍然不)理解按位运算符的目的。


在一个值中读取或写入一个或多个位组,而不会干扰另一个或多个b $ b位干扰,并且有效地执行此操作。


它允许你用机器整数表示(小)值集合。

我知道有时你可以转到
函数:

func(ONE | TWO | THREE);

通过这三个选项。我不明白的是
意味着什么,


如果func需要一组选项,那么ONE,TWO和THREE是值

表示(独立)选项集(通常是一个单独的
每个
),然后调用说取这些集合的/ union / - 即所有

值 - 并将其传递给func。

它正在做什么,以及函数如何知道你传递了一个,两个和三个而不是某个值等于ONE |两个|三个


它没有,也没关系。

和另一件事是如果ONE |三= =两个|四。然后怎样呢?


然后设计师可能会因为愚蠢的价值选择而受到谴责。或者

没有,如果没关系。

主要是我只是不明白为什么会使用面具,或为什么一个
bitiwise将使用运算符。我很确定我明白了什么是一个按位运算符我只是不知道为什么你需要它完成。
I have often times seen masks used in C source code. I really don''t
understand the purpose or functionality of this. I have seen used:

#define MASK 0x77

...

if(hello & MASK)

Also I don''t (yes still don''t) understand the purpose of the bitwise
operators.
To read or write a bit or groups of bits in a value without the other
bits being interfered with or interfering, and to do so efficiently.

It lets you represent (small) sets of values with machine integers.
I understand there are times when you may pass to a
function:

func(ONE | TWO | THREE);

To pass those three options. What I don''t understand is what that
means,
If func expects a set of options, and ONE, TWO, and THREE are values
that represent (independant) sets of options (typically a single one
each), then the call says to take the /union/ of those sets -- ie all
the values -- and pass that to func.
what it is doing, and how the function knows that you passed
ONE, TWO, and THREE and not some value just equal to ONE | TWO | THREE
It doesn''t, and it doesn''t matter either.
and another thing is what if ONE | THREE == TWO | FOUR. What then?
Then the designer may be berated for a silly choice of values. Or
not, if it doesn''t matter.
Mainly I just don''t understand WHY a mask would be used, or why a
bitiwise operator would be used. I''m pretty sure I understand what a
bitwise operator does I just don''t know why you would EVER need that
done.




把这个值想象成一堆比特(巧合的是,它们在大多数机器中都是什么),每个都有自己的特定含义。让's $




enum {READ = 0x1,WRITE = 0x2,EXECUTE = 0x4};


如果设置了位0,我们的意思是允许读取。 (有些东西,比如文件中的文件

系统,或内存映射中的页面),如果设置了第1位,我们的意思是允许写入,

和if第2位设置我们的意思是允许执行。


如果你想测试读取位:


if(value& READ)readBitSet();否则readBitUnset();


屏蔽意味着你/不关心/关于WRITE和EXECUTE位。


同样


value = value |写; / *或值| = WRITE * /


将打开WRITE位(如果已经设置,则保持设置)

/不更改/ READ和EXECUTE位。并且


value = value& 〜EXECUTE; / *或值& = ~EXECUTE * /


将关闭/关闭EXECUTE位(或关闭它,如果它已关闭

已经)没有触及其他人。


当值不是单个位时,有一些概括。

例如,如果你想描述一个在语法树中的节点,你可以使用一个字节,其中五个位为其类型,三个用于

其长度[1]然后


thatByte& 0x7


将为您提供长度字段(假设您将其存储在低端)




那个& 0xf8


将为您提供类型字段 - 您可能想要或不想转移




[1]当然,你需要以某种方式处理长节点,例如通过链接。


-

Chris寻求者 Dollin

你是谁?你想要什么? / Babylon 5 /



Think of the value as a bunch of bits (which, by coincidence, is what
they are in most machines), each with its own specific meaning. Let''s
say

enum { READ = 0x1, WRITE = 0x2, EXECUTE = 0x4 };

If bit 0 is set, we mean "allow reads" (of something, say files in a filing
system, or pages in a memory map), if bit 1 is set we mean "allow writes",
and if bit 2 is set we mean "allow execution".

If you want to test the read bit:

if (value & READ) readBitSet(); else readBitUnset();

The masking means that you /don''t care/ about the WRITE and EXECUTE bits.

Similarly

value = value | WRITE; /* or value |= WRITE */

will switch on the WRITE bit (or leave it set, if it were set already)
/without changing/ the READ and EXECUTE bits. And

value = value & ~EXECUTE; /* or value &= ~EXECUTE */

will switch /off/ the EXECUTE bit (or leave it off, if it were off
already) without touching the others.

There are generalisations for when the values aren''t just single bits.
For example, if you want to describe a node in a syntax tree, you
might use a single byte with five bits for its type and three for
its length [1] and then

thatByte & 0x7

will get you the length field (assuming you stored it at the low end)
and

thatByte & 0xf8

will get you the type field - which you may or may not want to shift
down.

[1] Of course you need to cope with long nodes somehow, eg by chaining.

--
Chris "seeker" Dollin
"Who are you? What do you want?" /Babylon 5/


2006-06-05 14:36,没有********* @ gmail.com 写道:
On 2006-06-05 14:36, no*********@gmail.com wrote:
我经常看到C源代码中使用的掩码。我真的不明白这个的目的或功能。我见过二手:

#define MASK 0x77


if(hello& MASK)


这用于仅传递部分部分。你好,更具体的所有位

你好,这些也是在MASK中设置的。

我也不知道(是的,我还是不理解)按位运算符的目的。我知道有些时候你可能会转到
函数:

func(ONE | TWO | THREE);

要通过这三个选项。我不明白的是,
意味着什么,它在做什么,以及该功能如何知道你通过了一,二和三而不是一些值等于一个|两个|三个


它不知道这个,结果就是它传递了一个值,

这个值是| - 操作适用于ONE,TWO和

三。在func的某个地方会有如下代码:


if(arg& ONE)

{

/ *做点什么* /

}


如果ONE通过,那将执行一些操作。

和另一件事是如果ONE |三= =两个|四。然后怎样呢?


麻烦,这个想法是这些面具中的每一个都会与

其他面具不同。通常,每个掩码只会设置一个位。对于

示例ONE可能有第一位设置而所有其他未设置,TWO有第二组

而所有其他未设置等等。

主要是我不明白为什么要使用面具,或为什么使用
bitiwise操作符。我很确定我明白了什么是一个按位运算符我只是不知道为什么你需要它完成。
I have often times seen masks used in C source code. I really don''t
understand the purpose or functionality of this. I have seen used:

#define MASK 0x77

...

if(hello & MASK)
This is used to pass only a "part" of hello, more specific all bits of
hello that are set that are also set in MASK.
Also I don''t (yes still don''t) understand the purpose of the bitwise
operators. I understand there are times when you may pass to a
function:

func(ONE | TWO | THREE);

To pass those three options. What I don''t understand is what that
means, what it is doing, and how the function knows that you passed
ONE, TWO, and THREE and not some value just equal to ONE | TWO | THREE
It does not know this, the result is that it''s one value that is passed,
and this value is the result of the |-operation applied to ONE, TWO and
THREE. Someplace in func there will be code like:

if (arg & ONE)
{
/* do something */
}

That will perform some operation if ONE was passed.
and another thing is what if ONE | THREE == TWO | FOUR. What then?
Trouble, the idea is that each of these masks will be different from the
others. Normally there will only be one bit set for each mask. For
example ONE might have the first bit set and all other unset, TWO have
the second set and all other unset and so on.
Mainly I just don''t understand WHY a mask would be used, or why a
bitiwise operator would be used. I''m pretty sure I understand what a
bitwise operator does I just don''t know why you would EVER need that
done.




请记住,C被发明的原因之一是在其中编写一个操作系统

,操作系统必须经常处理操作硬件和硬件

使用状态 - 必须读取或写入的寄存器。而这些

寄存器通常使用单独的位来指示状态。


在许多情况下,没有理由和个人玩耍。

位,使用一对普通的int并将它们设置为1或0的工作原理

也可以获得命名每个变量的好处。然而

用于其他用途,例如一些二进制格式(压缩会浮现在脑海中)

你必须能够修改单个位。


Erik Wikstr?m

-

我一直希望我的电脑和我的电脑一样好用

电话;我的愿望实现了,因为我再也无法想象如何使用我的电话了。 - Bjarne Stroustrup



Remember that one of the reasons that C was invented was to write an OS
in it, an OS have to deal with manipulating hardware and hardware often
use status-registers that have to be read or written. And these
registers generally use individual bits to signal status.

In many situations there is no reason to play around with individual
bits, using a a couple of normal int and setting them to 1 or 0 works
just as well and you get the benefit of naming each variable. However
for other uses such as some binary formats (compression comes to mind)
you have to be able to modify individual bits.

Erik Wikstr?m
--
"I have always wished for my computer to be as easy to use as my
telephone; my wish has come true because I can no longer figure
out how to use my telephone" -- Bjarne Stroustrup


这篇关于面具:为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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