镜像成像二进制数 [英] Mirror imaging binary numbers

查看:386
本文介绍了镜像成像二进制数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我正在尝试切换二进制数字,以便MSB成为

LSB等。有一种简单的方法吗?做到这一点,因为我似乎无法找到

任何东西。如果你能提供帮助那就太好了。谢谢,好吧

运气。

Craig

解决方案

Craig写道:


我正在尝试切换二进制数字,以便MSB变为

LSB等。


你的意思是'二进制数''?它们都是二进制的。如果你的意思是

int类型,它们是32位长,并且在MSB

和LSB(Most / Least Significant _Byte_)之间有16位。你想用最不重要的单词交换最多

重要单词吗?使用字节中最不重要的半字节交换最多

重要的半字节?或者你想要完全反转位顺序吗?


要在一个字节中交换半字节:


reverseVal =(val& 0xf)<< 4 | (val& 0xf0)> 4

基本上你需要使用位运算符(|,&,<<和>>)

来获得你需要的东西。如果你可能更具体,也许我可以获得更多帮助。




Matimus写道:


Craig写道:


我正在尝试切换二进制数字,以便MSB成为

LSB等。



你的意思是什么''二进制数''?它们都是二进制的。如果你的意思是

int类型,它们是32位长,并且在MSB

和LSB(Most / Least Significant _Byte_)之间有16位。你想用最不重要的单词交换最多

重要单词吗?使用字节中最不重要的半字节交换最多

重要的半字节?或者你想要完全反转位顺序吗?


要在一个字节中交换半字节:


reverseVal =(val& 0xf)<< 4 | (val& 0xf0)> 4

基本上你需要使用位运算符(|,&,<<和>>)

来获得你需要的东西。如果你可能更具体,也许我可以获得更多帮助。



非常感谢您的回复。我有一个单独的字节数组

这将最终组成一个二进制位图图像,加载到一个液晶屏幕上(1 =黑点,0 =白点) 。此刻每个字节

被反转到它应该是什么(完全反转位顺序):

例如00111101应该是10111100,11001100应该是00110011等等。

不是一个int问题,如果你得到

我的意思是更多一点水平交换。如果你能帮到那就太棒了。


2006-12-06,Craig< cr *********** *@gmail.com写道:


非常感谢您的回复。我有一个

单个字节的数组,最终将构成一个二进制位图

加载到LCD屏幕上的图像(1 =黑点,0 =

白点)。目前每个字节都反转为它应该是b $ b b(完全反转位顺序):例如00111101

应该是10111100,11001100应该是00110011等等。是
这不是一个int问题,如果你得到我的意思,它会更多一点水平交换。如果你能提供帮助那就太好了。



他已经告诉你90%的答案:使用位操作符

& | 〜^><<。


这里是答案的剩余10%(做了几个不同的

方式):


def showbits8(b):

mask = 0x80

而面具:

print" 01" ; [(b& mask)!= 0],

掩码>> = 1

打印


def bitswap8a(b):

r = 0

mask = 0x80

,而面具:

r>> = 1

如果b&面膜:

r | = 0x80

面膜>> = 1

返回r


def bitswap8b(b):

r = 0

为m1,m2为((0x80,0x01),(0x40,0x02),(0x20,0x04),(0x10) ,0x08),(0x01,0x80),(0x02,0x40),(0x04,0x20),(0x08,0x10)):

如果b& m1:

r | = m2

返回r

def testit(b):

showbits8(b)

showbits8(bitswap8a(b))

showbits8(bitswap8b(b))

打印


testit(0xc1)

testit(0x55)

testit(0xe2)


-

格兰特爱德华兹格兰特哇!这是BOOZE吗?

at

visi.com


Hi there,

I''m trying to switch binary numbers around so that the MSB becomes the
LSB etc. Is there an easy way of doing this as I can''t seem to find
anything. If you could help that would be great. Thanks and good
luck.
Craig

解决方案

Craig wrote:

I''m trying to switch binary numbers around so that the MSB becomes the
LSB etc.

What do you mean ''binary numbers''? They are all binary. If you mean the
int type, they are 32 bits long and there are 16 bits between the MSB
and LSB (Most/Least Significant _Byte_). Do you want to swap the most
significant word with the least significant word? Swap the most
significant nibble with the least significant nibble in a Byte? Or do
you want to completely reverse the bit order?

To swap nibbles in a byte:

reverseVal = (val & 0xf) << 4 | (val & 0xf0) >4

Basicly you are going to need to use the bit operators (|,&, << and >>)
to get what you need. If you could be more specific perhaps I could be
of more help.



Matimus wrote:

Craig wrote:

I''m trying to switch binary numbers around so that the MSB becomes the
LSB etc.


What do you mean ''binary numbers''? They are all binary. If you mean the
int type, they are 32 bits long and there are 16 bits between the MSB
and LSB (Most/Least Significant _Byte_). Do you want to swap the most
significant word with the least significant word? Swap the most
significant nibble with the least significant nibble in a Byte? Or do
you want to completely reverse the bit order?

To swap nibbles in a byte:

reverseVal = (val & 0xf) << 4 | (val & 0xf0) >4

Basicly you are going to need to use the bit operators (|,&, << and >>)
to get what you need. If you could be more specific perhaps I could be
of more help.

Thanks so much for the response. I have an array of individual bytes
which will eventually make up a binary bitmap image that is loaded onto
an LCD screen (1 = black dot, 0 = white dot). At the moment each byte
is reversed to what it should be (completely reverse the bit order):
e.g 00111101 should be 10111100, 11001100 should be 00110011, etc. It
is not an int problem as such, it is more a bit level swap if you get
what I mean. If you could help that would be great.


On 2006-12-06, Craig <cr************@gmail.comwrote:

Thanks so much for the response. I have an array of
individual bytes which will eventually make up a binary bitmap
image that is loaded onto an LCD screen (1 = black dot, 0 =
white dot). At the moment each byte is reversed to what it
should be (completely reverse the bit order): e.g 00111101
should be 10111100, 11001100 should be 00110011, etc. It is
not an int problem as such, it is more a bit level swap if you
get what I mean. If you could help that would be great.

He''s already told you 90% of the answer: use the bit operators
& | ~ ^ ><<.

Here''s the remaining 10% of the answer (done a couple different
ways):

def showbits8(b):
mask = 0x80
while mask:
print "01"[(b & mask) != 0],
mask >>= 1
print

def bitswap8a(b):
r = 0
mask = 0x80
while mask:
r >>= 1
if b & mask:
r |= 0x80
mask >>= 1
return r

def bitswap8b(b):
r = 0
for m1,m2 in ((0x80,0x01),(0x40,0x02),(0x20,0x04),(0x10,0x08),( 0x01,0x80),(0x02,0x40),(0x04,0x20),(0x08,0x10)):
if b & m1:
r |= m2
return r
def testit(b):
showbits8(b)
showbits8(bitswap8a(b))
showbits8(bitswap8b(b))
print

testit(0xc1)
testit(0x55)
testit(0xe2)


--
Grant Edwards grante Yow! Is this "BOOZE"?
at
visi.com


这篇关于镜像成像二进制数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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