反向位顺序 [英] reverse bit order

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

问题描述

有没有简单的方法来反转一个字节中的位的顺序?
C ++?


(即00000001变为10000000)

Is there any easy way to reverse the order of the bits in a byte in
C++?

(i.e. 00000001 becomes 10000000)

推荐答案

mi ****** @ gmail.com 写道:

是否有任何简单的方法来反转一个字节中的位顺序

C ++ ?


(即00000001变为10000000)
Is there any easy way to reverse the order of the bits in a byte in
C++?

(i.e. 00000001 becomes 10000000)

http://www.google.com/search?hl=en&q=reverse+bits+byte


V

-

请在通过电子邮件回复时删除资金''A'

我这样做没有回复最热门的回复,请不要问

http://www.google.com/search?hl=en&q=reverse+bits+byte

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


mi ******@gmail.com 写道:

有没有简单的方法来反转o

C ++中的一个字节的rder?


(即00000001变成10000000)
Is there any easy way to reverse the order of the bits in a byte in
C++?

(i.e. 00000001 becomes 10000000)



编号只有艰难的方法;)

你必须逐个移位。 />
最好的想法是计算一个包含反转位的256个值的数组



然后你可以查找在那个数组中。这里

是制作数组的代码(名为BitField):


int BitField [256];

for(int i = 0; i< 256; i ++)

{

int k = i;

BitField [i] = 0 ;

for(int b = 0; b< 8; b ++)

{

BitField [i]<< = 1 ;

BitField [i] | = k& 1;

k>> = 1;

}

}


如果你想要更安全的代码,使用向量而不是数组:

vector< intBitField(256);


祝你好运,马丁


PS通常,我不做其他人的作业 - SCNR这次;)

No. There is only the hard way ;)
You have to shift the bits one by one.
The best idea is to calculate an array
of 256 values that contain the reversed bits.
Then you can "look up" in that array. Here
is the code to make the array (named "BitField"):

int BitField[256];
for (int i = 0; i < 256; i++)
{
int k = i;
BitField[i] = 0;
for (int b = 0; b < 8; b++)
{
BitField[i] <<= 1;
BitField[i] |= k & 1;
k >>= 1;
}
}

If you want safer code, use a vector instead of an array:
vector<intBitField(256);

Best regards, Martin

P.S. normally, I don''t do other people''s homework - SCNR this time ;)


Mike发布:
Mike posted:

是否有任何简单的方法可以反转一个字节中的位的顺序

C ++?


(即00000001变为10000000)
Is there any easy way to reverse the order of the bits in a byte in
C++?

(i.e. 00000001 becomes 10000000)



这是我上个月写的一些代码。它是用C语言编写的,但你会得到

的想法:

http://groups.google.ie/group/comp.l...473fb7f?hl=en&


-


Frederick Gotham


Here''s some code I wrote last month. It''s written in C, but you''ll get the
idea:

http://groups.google.ie/group/comp.l...473fb7f?hl=en&

--

Frederick Gotham


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

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