C 中 char 类型的位交换
数据类型为 char,格式如下:
源字节:[0][1][2][3][4][5][6][7]
目标:[6][7][4] [5][2][3][0][1]
例如,如果我将 char, 29 传递给此函数,它将进行交换并返回 char 类型值,即 116。
我该如何做交换?
谢谢。
========================
只是想知道我是否可以这样做?
unsigned char mask = 128;
char num = 0, value1 = 29;
int i, a;
for(i = 0; i < 8; i++) {
if (i == 0 || i == 1 || i == 6 || i == 7)
a = 6;
else
a = 2;
if(i < 4)
num = ((value1 & mask) >> a);
else
num = ((value1 & mask) << a);
result = (result | num);
if(i<7)
mask = mask >> 1;
}
the data type is char, and the pattern is follow:
source byte: [0][1][2][3][4][5][6][7]
destination: [6][7][4][5][2][3][0][1]
for example, if I pass a char, 29 to this function, it will do the swapping and return a char type value, which is 116.
How can I do the swapping?
thank you.
========================
Just wondering if I can do in this way?
unsigned char mask = 128;
char num = 0, value1 = 29;
int i, a;
for(i = 0; i < 8; i++) {
if (i == 0 || i == 1 || i == 6 || i == 7)
a = 6;
else
a = 2;
if(i < 4)
num = ((value1 & mask) >> a);
else
num = ((value1 & mask) << a);
result = (result | num);
if(i<7)
mask = mask >> 1;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我通常以另一种方式对位进行编号——因此位 0 是 LSB。遵循您的编号方案:
当然,除非您“以正确的方式”对它们进行编号,但“向后”绘制它们 - 然后只需颠倒我上面所做的即可。 (并不是说我想在这里发动一场宗教战争......)
I usually number my bits the other way -- so that bit 0 is the LSB. Following your numbering scheme:
Unless of course, you're numbering them "the right way", but drawing them "backwards" -- then just reverse what I've done above. (Not that I'm trying to start a religious war here...)
或查找表
以防万一您不明白。这里有更多细节
对于 256 个可能的输入中的每一个,(手动)计算出答案,
然后
我是否要赶紧补充一下,我给出的值只是一个示例 - 并且肯定不正确
or a lookup table
just in case you dont understand that. Here is more detail
For each of the 256 possible inputs work out the answer (by hand)
then do
I hasten to add that the values I gave are an example - and are certainly not correct
请参阅位旋转黑客反转位序列”部分>。
另外,如果您想自己执行此操作:
要读取第 n 位:
int bit = value & (1 << n);
如果未设置该位,则bit
为 0。要设置第 n 位:
value |= 1 << n;
(value = value OR(1 移位 n 位))要清除第 n 位:
value &= ~(1 << n) ;
(值 = 值 AND NOT(1 移位 n 位))See the "Reversing bit sequences" section on Bit Twiddling Hacks.
Also, if you want to do it yourself:
To read the n-th bit:
int bit = value & (1 << n);
If the bit is not set,bit
is 0.To set the n-th bit:
value |= 1 << n;
(value = value OR (1 shifted by n digits))To clear the n-th bit:
value &= ~(1 << n);
(value = value AND NOT (1 shifted by n digits))首先将低四位与高四位交换,然后交换所有相邻的位对:
First swap the lower four bits with the higher four bits, then swap all adjacent pairs of bits:
您可能会发现这很有帮助:
http://graphics.stanford.edu/~seander/bithacks .html#BitReverseObvious
但它的位反转并不完全是您所需要的。只需做一点工作,您就可以更改“明显”的算法来完成您想要的操作。
You may find this helpful:
http://graphics.stanford.edu/~seander/bithacks.html#BitReverseObvious
but it the bit reversal there isn't exactly what you need. With just a little work you could change the "obvious" algorithm to do what you want.
实现:
调用
shiftit( 29 )
时返回 116。Implementation:
Returns 116 when called
shiftit( 29 )
.通过进位进行循环 http://en.wikipedia.org/wiki/Bitwise_operation#Rotate_through_carry
所以这会起作用:
Rotate through carry http://en.wikipedia.org/wiki/Bitwise_operation#Rotate_through_carry
So this would work: