如何操作和使用C重新present二进制数++ [英] How to manipulate and represent binary numbers in C++

查看:139
本文介绍了如何操作和使用C重新present二进制数++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在试图建立一个查找表使用pretty简单preorder遍历算法哈夫曼树,但我陷入开展非常基本的逐位操作。该伪code如下:

I'm currently trying to build a lookup table for a huffman tree using a pretty simple preorder traversal algorithm, but I'm getting stuck carrying out very basic bit wise operations. The psuedo code follows:

void preOrder(huffNode *node, int bit) //not sure how to represent bit
{
  if (node == NULL)
    return;

  (1) bit = bit + 0; //I basically want to add a 0 onto this number (01 would go to 010)
  preOrder(node->getLeft(), bit);
  (2) bit = bit - 0 + 1; //This should subtract the last 0 and add a 1 (010 would go to 011)
  preOrder(node->getRight());


}

我十分困惑如何开展线(1)定义的操作(2)

I'm getting quite confused about how to carry out the operations defined on lines (1) and (2)

什么数据类型类型做一个用重新present打印二进制数?在上面的例子中我已经重新psented为int $ P $数,但我pretty肯定是不正确。另外你怎么加或减值?我知道如何与放大器;和|逻辑类型的作品,但我越来越困惑,一个人如何进行这类在code操作。

What data type type does one use to represent and print binary numbers? In the above example I have the number represented as an int, but i'm pretty sure that that is incorrect. Also how do you add or subtract values? I understand how & and | types logic works, but I'm getting confused as to how one carries out these sorts of operations in code.

任何人都可以发布一些非常简单的例子?

Could anyone post some very simple examples?

推荐答案

下面是二进制运算的一些基本例子。我主要是用在这里发生的操作。

Here's some basic examples of binary operations. I've used mostly in-place operations here.

int bit = 0x02;   //               0010
bit |= 1;         // OR  0001 ->   0011
bit ^= 1;         // XOR 0001 ->   0010
bit ^= 7;         // XOR 0111 ->   0101
bit &= 14;        // AND 1110 ->   0100
bit <<= 1;        // LSHIFT 1 ->   1000
bit >>= 2;        // RSHIFT 2 ->   0010
bit = ~bit;       // COMPLEMENT -> 1101

如果您想打印你需要自己做一个二进制数...这里有一个稍微效率低下,而且适度的可读性,办法做到这一点:

If you want to print a binary number you need to do it yourself... Here's one slightly inefficient, but moderately readable, way to do it:

char bitstr[33] = {0};
for( int b = 0; b < 32; b++ ) {
    if( bit & (1 << (31-b)) )
        bitstr[b] = '1';
    else
        bitstr[b] = '0';
}
printf( "%s\n", bitstr );

如果我想更快的code,我可能pre-生成(或硬code)与所有数字8位序列的查找表从0到255。

[edit] If I wanted faster code, I might pre-generate (or hardcode) a lookup table with the 8-bit sequences for all numbers from 0-255.

// This turns a 32-bit integer into a binary string.
char lookup[256][9] = {
    "00000000",
    "00000001",
    "00000010",
    "00000011",
    // ... etc (you don't want to do this by hand)
    "11111111"
};

char * lolo = lookup[val & 0xff];
char * lohi = lookup[(val>>8) & 0xff];
char * hilo = lookup[(val>>16) & 0xff];
char * hihi = lookup[(val>>24) & 0xff];

// This part is maybe a bit lazy =)
char bitstr[33];
sprintf( "%s%s%s%s", hihi, hilo, lohi, lolo );

相反,你可以这样做:

Instead, you could do this:

char *bits = bitstr;
while( *hihi ) *bits++ = *hihi++;
while( *hilo ) *bits++ = *hilo++;
while( *lohi ) *bits++ = *lohi++;
while( *lolo ) *bits++ = *lolo++;
*bits = 0;

或者只是解开了整个事情。 ; - )

Or just unroll the whole thing. ;-)

char bitstr[33] = {
    hihi[0], hihi[1], hihi[2], hihi[3], hihi[4], hihi[5], hihi[6], hihi[7],
    hilo[0], hilo[1], hilo[2], hilo[3], hilo[4], hilo[5], hilo[6], hilo[7],
    lohi[0], lohi[1], lohi[2], lohi[3], lohi[4], lohi[5], lohi[6], lohi[7],
    lolo[0], lolo[1], lolo[2], lolo[3], lolo[4], lolo[5], lolo[6], lolo[7],
    0 };

当然,在查找这8个字节是相同的长度为64位整数...那么是什么样?远远高于一切毫无意义,通过字符数组蜿蜒更快。

Of course, those 8 bytes in the lookup are the same length as a 64-bit integer... So what about this? Much faster than all that pointless meandering through character arrays.

char bitstr[33];
__int64 * intbits = (__int64*)bitstr;
intbits[0] = *(__int64*)lookup[(val >> 24) & 0xff];
intbits[1] = *(__int64*)lookup[(val >> 16) & 0xff];
intbits[2] = *(__int64*)lookup[(val >> 8) & 0xff];
intbits[3] = *(__int64*)lookup[val & 0xff];
bitstr[32] = 0;

当然,在上面的code,你会重新present您的查找值作为64位整数,而不是字符串。

Naturally, in the above code you would represent your lookup values as int64 instead of strings.

总之,只是指出,你可以写但它是适合您的目的。如果您需要优化,事情变得有趣,但对于大多数实际应用这样的优化是可以忽略的或毫无意义的。

Anyway, just pointing out that you can write it however is appropriate for your purposes. If you need to optimize, things get fun, but for most practical applications such optimizations are negligible or pointless.

这篇关于如何操作和使用C重新present二进制数++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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