获取32位整数的位 [英] Getting at the bits of a 32-bit integer

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

问题描述

你好清单,


我是一名程序员,所以如果以下任何假设错误,请纠正我。


假设我有十进制数255.因为Python中的整数是32位/ b $ b位,所以它看起来像二进制:


00000000 00000000 00000000 11111111

数字左侧有大量未使用的位。如果我想要将这些位中的一些用作真/假标志,那么我该如何处理它?b $ b?在Python中,我如何编写最左边的

字节的代码,或者从32的集合左边的第三个位,或者

最右边的字节加上位于左侧等...


提前感谢您提供任何帮助。 :)


雅各布

Hi there list,

I''m a beginning programmer, so please correct me if any of the
following assumptions are wrong.

Suppose I have the decimal number 255. Since integers in Python are 32
bits, it would look like this in binary:

00000000 00000000 00000000 11111111

There are plenty of unused bits to the left of the number itself. If I
wanted to use some of these bits as true/false flags, how would I go
about it? In Python, how do I write code that gets at the leftmost
byte, or the third bit from the left of the set of 32, or the
rightmost byte plus the bit to its left, etc...

Thanks in advance for any help on this. :)

Jacob

推荐答案

如果你真的想这样做,你可以使用按位 ;和QUOT&;和'或'或$

运算符分别取消设置/设置位。例如,如果你想将
设置为第9位,则为二进制的0x0100或0000 0001 0000 0000。第9位未设置的整数

''我'是''i& ~0x0100''(〜是一个补码或

按位运算符;它反转所有位),''i | 0x0100''是''我'

,第9位设置。您还可以使用按位异或^。切换

有点像''i ^ 0x0100''。


然而,做这种事情是相当不敏感的。 Python

抽象平台的整数表示。整数在

Python并不总是32位;它们实际上是C'''long''
类型的大小,也就是说你可以通过查看sys.maxint找到它的大小,

但是它比它的价值更麻烦。如果你改变最高位,你也会遇到问题


If you really want to do that, you can use the bitwise "and" and "or"
operators to unset/set bits, respectivly. For example, if you want to
set bit 9, that''s 0x0100, or 0000 0001 0000 0000 in binary. The integer
''i'' with bit 9 unset is ''i & ~0x0100'' (~ is the one''s complement or
bitwise not operator; it inverts all the bits), and ''i | 0x0100'' is ''i''
with bit 9 set. You can also use the bitwise exclusive or "^" to toggle
a bit, like ''i ^ 0x0100''.

However, doing this sort of thing is rather nonpythonic. Python
abstracts the platform''s representation of an integer. Integers in
Python arn''t always 32 bits; they are actually the size of C''s ''long''
type, which is to say you can find the size by looking at sys.maxint,
but it''s more trouble than it''s worth. You will also encounter problems
if you change the highest bit:
10 ^ (1<< 31)
10 ^ (1 << 31)


__main __:1:FutureWarning:x<< y丢失位或更改符号将在Python 2.4中返回
a long及以上

-2147483638


请注意,错误信息甚至不准确。这可能是一个很好的

表示这不是常见的用例!


所以,短篇小说:使用bool(真/假)


2004年8月23日星期一下午04:52:07 -0700,Jacob H写道:你好清单,

我是一名程序员,所以如果下面的任何假设是错误的,请纠正我。

假设我有十进制数255.因为Python中的整数是32位,它看起来像这样二进制文件:

00000000 00000000 00000000 11111111

数字本身左侧有大量未使用的位。如果我想将这些位中的一些用作真/假标志,我该如何处理呢?在Python中,我如何编写最左边的字节,或者从32的左边的第三个位,或者最右边的字节加上左边的位等等。 。

提前感谢您对此的任何帮助。 :)

__main__:1: FutureWarning: x<<y losing bits or changing sign will return
a long in Python 2.4 and up
-2147483638

Note that the error message isn''t even accurate. This is probably a good
indication that this isn''t a common use case!

So, short story: use a bool (True/False)

On Mon, Aug 23, 2004 at 04:52:07PM -0700, Jacob H wrote: Hi there list,

I''m a beginning programmer, so please correct me if any of the
following assumptions are wrong.

Suppose I have the decimal number 255. Since integers in Python are 32
bits, it would look like this in binary:

00000000 00000000 00000000 11111111

There are plenty of unused bits to the left of the number itself. If I
wanted to use some of these bits as true/false flags, how would I go
about it? In Python, how do I write code that gets at the leftmost
byte, or the third bit from the left of the set of 32, or the
rightmost byte plus the bit to its left, etc...

Thanks in advance for any help on this. :)



ja ****** **@postmark.net (Jacob H)写道:
ja********@postmark.net (Jacob H) writes:
数字本身左侧有大量未使用的位。如果我想将这些位中的一些用作真/假标志,我该如何处理呢?在Python中,我如何编写最左边的字节,或者从32的左边的第三个位,或者最右边的字节加上左边的位等等。 。
There are plenty of unused bits to the left of the number itself. If I
wanted to use some of these bits as true/false flags, how would I go
about it? In Python, how do I write code that gets at the leftmost
byte, or the third bit from the left of the set of 32, or the
rightmost byte plus the bit to its left, etc...




它不是真正的Python精神,但一般来说你可以设置

nth位(从右边开始,从变量x的n = 0开始,说:


x | =(1<< n)


你可以清除它与


x& =〜(1<< n)


你可以用
进行测试
如果x& (1<< n):

做任何事情



It''s not really in the Python spirit, but in general you can set the
nth bit (from the right, starting at n=0) of variable x by saying:

x |= (1 << n)

You can clear it with

x &= ~(1 << n)

and you can test it with

if x & (1 << n):
do whatever


2004年8月23日星期一16:52:07 -0700,Jacob H写道:
On Mon, 23 Aug 2004 16:52:07 -0700, Jacob H wrote:
00000000 00000000 00000000 11111111

数字本身左侧有大量未使用的位。如果我想将这些位中的一些用作真/假标志,我该怎么做呢?在Python中,如何编写最左边字节的代码,或者从32的左边开始的第三个位,或者最右边的字节加上左边的
位等等。 。
00000000 00000000 00000000 11111111

There are plenty of unused bits to the left of the number itself. If I
wanted to use some of these bits as true/false flags, how would I go about
it? In Python, how do I write code that gets at the leftmost byte, or the
third bit from the left of the set of 32, or the rightmost byte plus the
bit to its left, etc...




嗯,基本答案与C相同,有&,>>,并且适当的

常量为什么你正在做。



Well, the basic answer is the same as C, with &, >>, and appropriate
constants for what you are doing.

#access the 10th
a = 1025
(a & 1024)>> 10
1 a = 1023
(a& 1024)>> 10
0


显然,你需要更好的常量和东西。


但是,我必须挑战你需要这样做第一个地方。

Python和位杂乱的人通常不会在一起;即使你是访问二进制文件或协议的
,你也应尽可能多地对

struct(?)模块进行随机播放。如果你想跟踪每一个比特和字节,

Python对你来说是错误的语言......除非你跟踪许多数百万比特的b
,这就是错误的十年吧。


使用一个类,并为其设置成员为true或false。

class DummyStruct:pass
flags = DummyStruct()
flags.aFlag = True
#access the 10th bit
a = 1025
(a & 1024) >> 10 1 a = 1023
(a & 1024) >> 10 0

Obviously, you need better constants and stuff.

However, I must challenge your need to do this in the first place.
Python and bit twiddling generally don''t go together; even if you are
accessing a binary file or protocol you should shuffle out as much to the
struct (?) module as possible. If you want to track every bit and byte,
Python is the wrong language for you... and unless you are tracking many
many millions of bits, this is the wrong decade for it.

Use a class, and set members on it to true or false for your flags.
class DummyStruct: pass
flags = DummyStruct()
flags.aFlag = True




(这类课程的好处是你经常会发现顺利地将它们转换成真正*有用的*类,强调复数的类:-)。)


有点笨拙很可能是*比这更慢*,除非

你正在处理数百万这些,节省的内存将是zilch。


如果你*是*处理数百万这些,请务必查看

数组。模块。



(The nice thing about such classes is you will often find them smoothly
transition into really *useful* classes, emphasis on the plural "classes" :-).)

The bit twiddling will most likely be *much* slower than this, and unless
you''re dealing with millions of these, the memory savings will be zilch.

If you *are* dealing with millions of these, be sure to look at the
"array" module.


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

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