C 中 char 类型的位交换

发布于 2024-09-27 07:36:05 字数 615 浏览 1 评论 0原文

数据类型为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

小瓶盖 2024-10-04 07:36:05

我通常以另一种方式对位进行编号——因此位 0 是 LSB。遵循您的编号方案:

unsigned char src = 29;
unsigned char dst = 0;
dst = (((src & 0x80) >> 6) | // bit 0
       ((src & 0x40) >> 6) | // bit 1
       ((src & 0x20) >> 2) | // bit 2
       ((src & 0x10) >> 2) | // bit 3
       ((src & 0x08) << 2) | // bit 4
       ((src & 0x04) << 2) | // bit 5
       ((src & 0x02) << 6) | // bit 6
       ((src & 0x01) << 6) // bit 7
      );

当然,除非您“以正确的方式”对它们进行编号,但“向后”绘制它们 - 然后只需颠倒我上面所做的即可。 (并不是说我想在这里发动一场宗教战争......)

I usually number my bits the other way -- so that bit 0 is the LSB. Following your numbering scheme:

unsigned char src = 29;
unsigned char dst = 0;
dst = (((src & 0x80) >> 6) | // bit 0
       ((src & 0x40) >> 6) | // bit 1
       ((src & 0x20) >> 2) | // bit 2
       ((src & 0x10) >> 2) | // bit 3
       ((src & 0x08) << 2) | // bit 4
       ((src & 0x04) << 2) | // bit 5
       ((src & 0x02) << 6) | // bit 6
       ((src & 0x01) << 6) // bit 7
      );

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

木槿暧夏七纪年 2024-10-04 07:36:05

或查找表

以防万一您不明白。这里有更多细节

对于 256 个可能的输入中的每一个,(手动)计算出答案,

然后

unsigned char answers[256] = {0x00, 0x40,0x21.....};
unsigned char answer = answers[input];

我是否要赶紧补充一下,我给出的值只是一个示例 - 并且肯定不正确

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

unsigned char answers[256] = {0x00, 0x40,0x21.....};
unsigned char answer = answers[input];

I hasten to add that the values I gave are an example - and are certainly not correct

挽你眉间 2024-10-04 07:36:05

请参阅位旋转黑客反转位序列”部分>。

另外,如果您想自己执行此操作:

要读取第 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))

音盲 2024-10-04 07:36:05

首先将低四位与高四位交换,然后交换所有相邻的位对:

dst = src;
dst = ((dst & 0xF0) >> 4) | ((dst & 0x0F) << 4);
dst = ((dst & 0xCC) >> 2) | ((dst & 0x33) << 2);

First swap the lower four bits with the higher four bits, then swap all adjacent pairs of bits:

dst = src;
dst = ((dst & 0xF0) >> 4) | ((dst & 0x0F) << 4);
dst = ((dst & 0xCC) >> 2) | ((dst & 0x33) << 2);
伴我心暖 2024-10-04 07:36:05

您可能会发现这很有帮助:

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.

夏末 2024-10-04 07:36:05
source byte: [01][23][45][67] to
destination: [67][45][23][01]

实现:

unsigned char shiftit( unsigned char in ) {
  unsigned char out;

  out = (
    (( in & 0xC0 ) >> 6) + /* top 2 to bottom 2 */
    (( in & 0x30 ) >> 2) + /* centre-left 2 to centre-right */
    (( in & 0x0C ) << 2) + /* centre-right 2 to centre-left */
    (( in & 0x03 ) << 6)   /* bottom 2 to top 2 */
  );

  return( out );
}

调用 shiftit( 29 ) 时返回 116。

source byte: [01][23][45][67] to
destination: [67][45][23][01]

Implementation:

unsigned char shiftit( unsigned char in ) {
  unsigned char out;

  out = (
    (( in & 0xC0 ) >> 6) + /* top 2 to bottom 2 */
    (( in & 0x30 ) >> 2) + /* centre-left 2 to centre-right */
    (( in & 0x0C ) << 2) + /* centre-right 2 to centre-left */
    (( in & 0x03 ) << 6)   /* bottom 2 to top 2 */
  );

  return( out );
}

Returns 116 when called shiftit( 29 ).

廻憶裏菂餘溫 2024-10-04 07:36:05

通过进位进行循环 http://en.wikipedia.org/wiki/Bitwise_operation#Rotate_through_carry

所以这会起作用:

myByte = myByte << 2 | myByte >> 6;

Rotate through carry http://en.wikipedia.org/wiki/Bitwise_operation#Rotate_through_carry

So this would work:

myByte = myByte << 2 | myByte >> 6;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文