python 32位整数,获取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

解决方案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)

__main__:1: FutureWarning: x<

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) writes: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...

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

On Mon, 23 Aug 2004 16:52:07 -0700, Jacob H wrote: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...

Well, the basic answer is the same as C, with &, >>, and appropriate

constants for what you are doing.

#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

(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.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值