需要有关位操作定义的帮助 [英] Need help on bit operations define

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

问题描述

我无法理解以下定义的作用。可以

任何人提供一些帮助吗?


#define SetBits(bits,pos)(((bits)[(pos)>> 3]) | =(1<(&((((((((((((((((((((((((((((((((((审核 - 审核地址: cl**@plethora.net

解决方案



" MakisGR" <是******* @ hotmail.com>在留言中写道

news:cl **************** @ plethora.net ...

我有无法理解以下定义的作用。可以
任何人提供一些帮助吗?

#define SetBits(bits,pos)(((位)[(pos)>> 3])| =(1<< ((pos)&
7)))




1)它假设传递的参数''bits''是指针或数组。


2)它假设''pos''是一种适合用作数组的类型

索引或指针算术(例如size_t)。 br />

3)在地址''位+(pos / 8)''[* 1]评估对象


4)按位ANDs [ * 2]''pos''用7(通过最高位将位从第2位[* 3]

设置为零)


)计算2的值来自4)


5)按位OR [* 4]来自3)和4)的两个值,并存储

导致''位[pos / 8]''

[1]将二进制值向右移一位将其除以2,

shift左边乘以2。所以''pos>> 3''与''pos / 2 ^ 3''和''pos / 8''相同

。为了优化的利益,有些人使用转换而不是
的乘法和除法,但是使用今天的现代优化编译器来获得
,例如''聪明' '

通常不是必需的,在某些情况下是有害的。


[2]

按位AND:

逐位比较两个操作数。


如果每个操作数的相应位都是1,则

结果为1(对于该位) 。如果其中一个或两个都是
为零,则结果为零(对于该位)。


AND 0 1

- ---------

0 0 | 0

-----------

1 0 | 1

[3]将最低位用作''位零''


[4]

按位OR:

逐位比较两个操作数。


如果每个操作数的相应位中的任何一个(或两个)为1,则

结果是1(对于那个位)。如果两者都为零,结果是

零(对于那个位)。


OR 0 1

---- -------

0 0 | 1

-----------

1 1 | 1


HTH,

-Mike


MakisGR写道:


我无法理解以下定义的作用。可以
任何人提供一些帮助吗?

#define SetBits(bits,pos)(((位)[(pos)>> 3])| =(1<< ((pos)& 7)))




(pos)>> 3英寸快速除以8.


(1<((pos)& 7))"引用位(pos)-modulo-8。换句话说,

0 ==> 00000001

1 ==> 00000010

2 ==> 00000100

3 ==> 00001000

4 ==> 00010000

5 ==> 00100000

6 ==> 01000000

7 ==> 10000000


假设(位)是一个8位或更多位字符的数组,用于保存8 * n

位,它将设置(这是| =部分)位pos在数组中。


-

+ ----------------------- - + -------------------- + -------------------------- --- +

| Kenneth J. Brody | www.hvcomputer.com | |

| kenbrody / at\spamcop.net | www.fptech.com | #include< std_disclaimer.h> |

+ ------------------------- + -------------- ------ + ----------------------------- +

-

comp.lang.c.moderated - 审核地址: cl**@plethora.net


你好,


F''Up to comp.lang.c

# define SetBits(bits,pos)(((bits)[(pos)>> 3])| =(1<(<((pos)&
7)))




不在宏中引入换行符;更好:

#define SetBits(bits,pos)(((bits)[(pos)>> 3])| = \

(1< <((pos)& 7)))





#define SetBits(bits,pos)\

(((位)[(pos)>> 3])| =(1<<((pos)& 7)))


让''s'解析它:

有一个赋值| =,即我们也可以写


(((位)[(pos)>> 3]) =((位)[(pos)>> 3])|(1<(<((pos)& 7)))


现在,做什么( (位)[(pos)>> 3])意思是?

我们可以假设(位)是指向某个对象的指针

它是对象在地址

地址(位)加((pos)>>对象大小的3倍(位)

指向)


(pos)>> 3表示取(pos)的二进制表示并将其移动到

三向右(即扔掉最后三个)二进制数字和

从左边填充三位数。)

(pos)必须是无符号类型。

最低三位(pos)不在这里使用。


右侧|:

(1<<((pos)& 7))

拿一个二进制文件,它仍然是一个并将它移到左边的

(pos)& 7,即附加(pos)&在右边7个零并扔掉

(pos)&左起7位数。

(pos)& 7表示将(pos)的所有位设置为零,其中对应的7位(十进制)位为零。 7(十进制)是111(二进制),

因此我们基本上只考虑(pos)的最低三位

之前未使用过。这意味着我们可以在0到7位之间向左移动。


我们假设(位)是指向无符号字符数组的指针

其中包含8位和(pos)是一个unsigned int,然后我们可以直接使用
地址任意一点。如果(pos)= n,其中n = 8 * i + j,

0< = j< 8,则SetBits(bits,pos)设置第n位,即,

从开始到第i个字节的第一位。


所以,你基本上可以将(位)解释为二进制数任意

长度,你可以使用

SetBits将任意数字设置为1。这意味着与使用数组的每个元素作为二进制数字使用

相比,您可以节省因子8。然后,你会用
使用(位)[(pos)] = 1而不是SetBits(位,pos)。


如果你遇到问题二元运算符,我们可以消除一些它们的b $ b:


#define SetBits(bits,pos)(((bits)[(pos)/ 8] )| = \

(1<<((pos)%8)))

如果CHAR_BIT不是8,你主要想要拥有最大内存

保存,然后用CHAR_BIT替换8:


#define SetBits(bits,pos)(((bits)[(pos) / CHAR_BIT])| = \

(1<<<((pos)%CHAR_BIT)))

顺便说一下:你这里只设置一位,所以SetBit将

为宏的更好名称。

干杯,

Michael

-

comp.lang.c.moderated - 审核地址: cl**@plethora.net


I''m having trouble understanding what the following define does. Can
anyone provide some assistance?

#define SetBits(bits,pos) (((bits)[(pos) >> 3]) |= (1 << ((pos) &
7)))
--
comp.lang.c.moderated - moderation address: cl**@plethora.net

解决方案


"MakisGR" <be*******@hotmail.com> wrote in message
news:cl****************@plethora.net...

I''m having trouble understanding what the following define does. Can
anyone provide some assistance?

#define SetBits(bits,pos) (((bits)[(pos) >> 3]) |= (1 << ((pos) &
7)))



1) It assumes that the passed argument ''bits'' is a pointer or an array.

2) It assumes that ''pos'' is of a type suitable for use as an array
index or pointer arithmetic (e.g. size_t).

3) Evaluates the object at address ''bits + (pos / 8)'' [*1]

4) Bitwise ANDs [*2] ''pos'' with 7 (which will set bits from bit 2 [*3]
through the highest order bit, to zero)

4) Calculates 2 to the power of the value from 4)

5) Bitwise ORs [*4] the two values from 3) and 4), and stores the
result in ''bits[pos / 8]''
[1] Shifting binary value one digit to the right divides it by two,
shifting left multiplies by two. So ''pos >> 3'' is the same
as ''pos / 2^3'', and ''pos / 8''. Some folks use shifting instead
of multiply and divide in the interest of ''optimization'', but
with today''s modern optimizing compilers, such ''cleverness''
is not typically necessary, and in some cases detrimental.

[2]
Bitwise AND:
Compares two operands bit by bit.

If both corresponding bits from each operand are one,
the result is 1 (for that bit). If either or both are
zero, the result is zero (for that bit).

AND 0 1
-----------
0 0 | 0
-----------
1 0 | 1
[3] Using the lowest order bit as ''bit zero''

[4]
Bitwise OR:
Compares two operands bit by bit.

If either (or both) of corresponding bits from each operand is one,
the result is 1 (for that bit). If both are zero, the result is
zero (for that bit).

OR 0 1
-----------
0 0 | 1
-----------
1 1 | 1

HTH,
-Mike


MakisGR wrote:


I''m having trouble understanding what the following define does. Can
anyone provide some assistance?

#define SetBits(bits,pos) (((bits)[(pos) >> 3]) |= (1 << ((pos) & 7)))



The "(pos) >> 3" is a quick divide-by-8.

The "(1 << ((pos) & 7))" references bit (pos)-modulo-8. In other words,
0 ==> 00000001
1 ==> 00000010
2 ==> 00000100
3 ==> 00001000
4 ==> 00010000
5 ==> 00100000
6 ==> 01000000
7 ==> 10000000

Assuming that (bits) is an array of 8-or-more-bit chars, used to hold 8*n
bits, it will set (that''s the "|=" part) bit "pos" in the array.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
--
comp.lang.c.moderated - moderation address: cl**@plethora.net


Hi there,

F''Up to comp.lang.c

#define SetBits(bits,pos) (((bits)[(pos) >> 3]) |= (1 << ((pos) &
7)))



do not introduce linebreaks in macros; better:
#define SetBits(bits,pos) (((bits)[(pos) >> 3]) |= \
(1 << ((pos) & 7)))

or

#define SetBits(bits,pos) \
(((bits)[(pos) >> 3]) |= (1 << ((pos) & 7)))

Let''s "parse" it:
There is an assignment |=, i.e. we could also write

(((bits)[(pos) >> 3]) = ((bits)[(pos) >> 3]) | (1 << ((pos) & 7)))

Now, what does ((bits)[(pos) >> 3]) mean?
We can assume that (bits) is a pointer to a certain object
It is the object at the address
address of (bits) plus ( (pos)>>3 times the size of the object (bits)
points to )

(pos)>>3 means "take the binary representation of (pos) and shift it by
three to the right (i.e. throw away the last three binary digits and
fill three digits from the left).
(pos) must be of an unsigned type.
The lowest three bits of (pos) are not used here.

Right side of |:
(1<< ((pos) & 7) )
Take one in binary, that is still one and shift it to the left by
(pos) & 7, i.e. append (pos) & 7 zeros at the right and throw away
(pos) & 7 digits from the left.
(pos) & 7 means "set to zero all bits of (pos) for which the
corresponding bits of 7(decimal) are zero. 7(decimal) is 111(binary),
so we essentially consider only the lowest three bits of (pos) which
went unused before. That means we can shift left between 0 and 7 digits.

Let us assume that (bits) was a pointer to an array of unsigned chars
which consist of 8 bits and (pos) was an unsigned int, then we could
directly "address" an arbitrary bit. If (pos) = n, where n=8*i+j,
0<=j<8, then SetBits(bits,pos) sets the n''th bit, that is, the
j''th bit of the i''th byte from the start to be one.

So, you essentially can interpret (bits) as a binary number of arbitrary
length for which you can set an arbitrary digit to 1 by using
SetBits. This means you can save factor 8 compared with using
every element of your array as a binary digit. Then, you would
use (bits)[(pos)]=1 instead of SetBits(bits,pos).

If you have problems with the binary operators, we can eliminate some
of them:

#define SetBits(bits,pos) (((bits)[(pos)/8]) |=\
(1 << ((pos)%8)))

If CHAR_BIT is not 8, and you mainly want to have the maximum memory
save, then replace 8 by CHAR_BIT:

#define SetBits(bits,pos) (((bits)[(pos)/CHAR_BIT]) |=\
(1 << ((pos)%CHAR_BIT)))
By the way: You are only setting one bit here, so SetBit would
be a better name for the macro.
Cheers,
Michael
--
comp.lang.c.moderated - moderation address: cl**@plethora.net


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

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