位操作 [英] Bit Operations

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

问题描述

你好,

我是陌生的新手(来自.net,请原谅我这个愚蠢的问题)

我想要做一个非常简单的事情,用字节。


我的问题是:


我有一个自然由2组成的字节nibbles hi& low,和两个

chars ..就像A和B一样。我想做的就是给高位半字节写A和B $ / b
给低位半字节。

或者另一个例子可以是我有2个数字..比如7和8并且要做的是和brs一样的



我真的很困惑怎么做,也许导致python是无类型的(动态

打字)

任何帮助?


干杯,

Gianmaria

意大利

解决方案

< blockquote>我真的很困惑如何做,可能导致python是


无类型(动态类型)



被鸭子打字并没有真正与它有任何关系。

Python支持逻辑移位和组合


我有一个自然的字节由2个半字节组成的高,低,

和两个字符......就像A和B.我想做的是写A给

High nibble和B to较低的蚕食。或者另外一个

的例子可以是我有2个数字......比如7和8并且要做什么

和chars相同。


>> a = int('''1001'', 2)
b = int(''0110'',2)
a



9


>> b



6


>> 0xff& (((0xff& a)<<<< 4)|(0xff& b))



150

或者,如果你马虎,


>>(a <<< 4)| b



150


并且用于验证:


>> int(''10010110'',2)



150


因此,可以将其作为函数包装


>> nibbles2byte = lambda a,b:



0xff& ; (((0xff& a)<<<<< 4)|(0xff& b))


>> nibbles2byte(a,b)



150

-tkc



2007年11月28日下午2:07,Gianmaria Iaculo - NVENTA

< gi *** **** @ hotmail.com写的:


你好,

我是陌生的新手(来自.net所以请原谅我这个愚蠢的问题。

我想做一个非常简单的事情,有字节。


我的问题是:


我有一个字节,自然是由2个半字节组成的高和低,以及两个

字符...就像A和B.我想做的是将A写入高位半字节,将B $ / b $ b写入低位半字节。



python中的字符串是一个字节序列,所以你在描述

这里是字符串AB。


或者另一个例子可以是我有2个数字..比如7和8并且要做的是

与chars。



" \ x07 \ x08"


我真的很困惑怎么做,可能导致python是无类型的(动态

类型)



您可以使用struct模块转换回来在字节

序列和数值之间。例如,要获得一个带有

的整数,您之前提到的半字节值:


struct.unpack(" h"," AB" ) - (16961,)

你想要使用什么以及你想要什么样的格式将取决于你为什么要这样做?


2007年11月28日下午2:27,Chris Mellon< ar ***** @ gmail.comwrote:


2007年11月28日下午2:07,Gianmaria Iaculo - NVENTA

< gi ******* @ hotmail.comwrote:


你好,

我是陌生的新手(来自.net,请原谅我这个愚蠢的问题)

我要做一个非常简单的事情,用字节。


我的问题是:


我有一个字节,自然是由2个半字节组成的高和低,和两个

字符...像A和B.我想做的是写A给高位半字节和B

到较低的半字节。



python中的字符串是一个字节序列,所以你要描述的是

这里是字符串AB。



啊,直到我发送了你之后我还没有意识到你试图将b $ b合并到同一个字节。这并没有多大意义

- ord(A)超出你可以用半个字节表示的范围 - 但是

Python确实支持全方位的按位操作,因此你可以做任何类型的移动和设置,你在.NET中做了什么。


Hi there,
I''m so new to python (coming from .net so excuse me for the stupid question)
and i''m tring to do a very simple thing,with bytes.

My problem is this:

i''ve a byte that naturally is composed from 2 nibbles hi&low, and two
chars.. like A nd B. What i wonna do is to write A to the High nibble and B
to the the lower nibble.
Or an other example can be i''ve 2 numbers.. like 7 and 8 and whant to do the
same as for chars.

I''m really confused on how t do it, maybe cause python is type-less (dynamic
typed)
Any Help?

Cheers,
Gianmaria
ITALY

解决方案

I''m really confused on how t do it, maybe cause python is

type-less (dynamic typed)

Being duck-typed doesn''t really have anything to do with it.
Python supports logical shifting and combining

i''ve a byte that naturally is composed from 2 nibbles hi&low,
and two chars.. like A nd B. What i wonna do is to write A to
the High nibble and B to the the lower nibble. Or an other
example can be i''ve 2 numbers.. like 7 and 8 and whant to do
the same as for chars.

>>a = int(''1001'', 2)
b = int(''0110'', 2)
a

9

>>b

6

>>0xff & (((0xff & a) << 4) | (0xff & b))

150

or, if you''re sloppy,

>>(a << 4) | b

150

And for verification:

>>int(''10010110'', 2)

150

Thus, that can be wrapped up as a function

>>nibbles2byte = lambda a,b:

0xff & (((0xff & a) << 4) | (0xff & b))

>>nibbles2byte(a,b)

150
-tkc



On Nov 28, 2007 2:07 PM, Gianmaria Iaculo - NVENTA
<gi*******@hotmail.comwrote:

Hi there,
I''m so new to python (coming from .net so excuse me for the stupid question)
and i''m tring to do a very simple thing,with bytes.

My problem is this:

i''ve a byte that naturally is composed from 2 nibbles hi&low, and two
chars.. like A nd B. What i wonna do is to write A to the High nibble and B
to the the lower nibble.

A string in python is a sequence of bytes, so what you''re describing
here is the string "AB".

Or an other example can be i''ve 2 numbers.. like 7 and 8 and whant to do the
same as for chars.

"\x07\x08"

I''m really confused on how t do it, maybe cause python is type-less (dynamic
typed)

You can use the struct module to convert back and forth between byte
sequences and numerical values. For example, to get an integer with
the value of the nibble you mentioned before:

struct.unpack("h", "AB") -(16961,)

Exactly what you''ll want to use and what format you want will depend
on why you''re doing this.


On Nov 28, 2007 2:27 PM, Chris Mellon <ar*****@gmail.comwrote:

On Nov 28, 2007 2:07 PM, Gianmaria Iaculo - NVENTA
<gi*******@hotmail.comwrote:

Hi there,
I''m so new to python (coming from .net so excuse me for the stupid question)
and i''m tring to do a very simple thing,with bytes.

My problem is this:

i''ve a byte that naturally is composed from 2 nibbles hi&low, and two
chars.. like A nd B. What i wonna do is to write A to the High nibble and B
to the the lower nibble.


A string in python is a sequence of bytes, so what you''re describing
here is the string "AB".

Ah, I didn''t realize until after I''d sent this that you were trying to
merge them into the same byte. This doesn''t make a whole lot of sense
- ord("A") is outside the range you can represent in half a byte - but
Python does support the full range of bitwise operations, so you can
do whatever kind of shifting and setting that you''d have done in .NET.


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

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