C / C ++位阵列或位向量 [英] C/C++ Bit Array or Bit Vector

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

问题描述

我学习C / C ++编程和放大器;曾经遇到'位阵列或位向量的使用。我无法理解他们的目的是什么?这里有我的疑惑 -

I am learning C/C++ programming & have encountered the usage of 'Bit arrays' or 'Bit Vectors'. Am not able to understand their purpose? here are my doubts -


  1. 使用它们作为布尔标志?

  2. 可以在一个使用 INT 数组来代替? (当然更多的内存,但..)

  3. 什么是位掩码的概念?

  4. 如果位掩码是简单的位操作,​​以获得相应的标志,如何为他们做一个节目?是不是很难做到这一点的操作头看升旗是什么,结合在十进制数?

  1. Are they used as boolean flags?
  2. Can one use int arrays instead? (more memory of course, but..)
  3. What's this concept of Bit-Masking?
  4. If bit-masking is simple bit operations to get an appropriate flag, how do one program for them? is it not difficult to do this operation in head to see what the flag would be, as apposed to decimal numbers?

我要寻找的应用程序,让我能更好地理解。对于EG -

I am looking for applications, so that I can understand better. for Eg -

问:您给出包含在范围(1到1万美元)的整数的文件。有一些重复的,因此一些数据丢失。查找寻找失踪的最快方法
号码?

Q. You are given a file containing integers in the range (1 to 1 million). There are some duplicates and hence some numbers are missing. Find the fastest way of finding missing numbers?

有关上述问题,我已阅读解决方案,告诉我要使用bit数组。一个人怎么会每个整数存储在一个位?

For the above question, I have read solutions telling me to use bit arrays. How would one store each integer in a bit?

推荐答案

我想你也得为自己的阵列和数字之间迷茫,具体是什么意思操纵二进制数。

I think you've got yourself confused between arrays and numbers, specifically what it means to manipulate binary numbers.

我会去的例子这个。假设你有一些错误消息,并要在从函数的返回值归还。现在,你可能标记你的错误,1,2,3,4 ......这是有道理的你的心,但你怎么了,给出的一个数字,找出其中的错误已经发生?

I'll go about this by example. Say you have a number of error messages and you want to return them in a return value from a function. Now, you might label your errors 1,2,3,4... which makes sense to your mind, but then how do you, given just one number, work out which errors have occured?

现在,试着标注错误1,2,4,8,16 ......越来越多的两个大国,基本上是这样。为什么这项工作?那么,当你的工作基地2你操纵一些像 00000000 ,其中每个数字对应于乘以从右边的位置是2的幂。所以我们可以说误区1,4和8发生。 psented为好了,那么可以重新$ P $ 00001101 。相反,第一位数= 1 * 2 ^ 0,第三个数字1 * 2 ^ 2和第四位1 * 2 ^ 3。如果把它们加起来给你13。

Now, try labelling the errors 1,2,4,8,16... increasing powers of two, basically. Why does this work? Well, when you work base 2 you are manipulating a number like 00000000 where each digit corresponds to a power of 2 multiplied by its position from the right. So let's say errors 1, 4 and 8 occur. Well, then that could be represented as 00001101. In reverse, the first digit = 1*2^0, the third digit 1*2^2 and the fourth digit 1*2^3. Adding them all up gives you 13.

现在,我们可以测试是否这样的错误已经通过应用掩码发生。举例,如果你想锻炼身体,如果误差 8 已发生,使用比特重8 = 00001000 。现在,为了提取该错误是否已经发生,请使用二进制和像这样:

Now, we are able to test if such an error has occured by applying a bitmask. By example, if you wanted to work out if error 8 has occured, use the bit representation of 8 = 00001000. Now, in order to extract whether or not that error has occured, use a binary and like so:

  00001101
& 00001000
= 00001000

我敢肯定,你知道如何和一个工程或从上述推断出这一点 - 工作位明智的,如果有两个数字都为1,则结果为1,否则为0

I'm sure you know how an and works or can deduce it from the above - working digit-wise, if any two digits are both 1, the result is 1, else it is 0.

现在,在C:

int func(...)
{
    int retval = 0;

    if ( sometestthatmeans an error )
    {
        retval += 1;
    }


    if ( sometestthatmeans an error )
    {
        retval += 2;
    }
    return retval
}

int anotherfunc(...)
{
    uint8_t x = func(...)

    /* binary and with 8 and shift 3 plaes to the right
     * so that the resultant expression is either 1 or 0 */
    if ( ( ( x & 0x08 ) >> 3 ) == 1 )
    {
        /* that error occurred */
    }
}

现在,到实用性。当内存稀疏和协议,没有冗长的XML等奢侈品,它是共同划定一个字段是这么多的位宽。在这一领域,可以分配不同的位(标志2的幂),以一定的含义和应用二进制运算来推断,如果它们设置,然后在这些工作。

Now, to practicalities. When memory was sparse and protocols didn't have the luxury of verbose xml etc, it was common to delimit a field as being so many bits wide. In that field, you assign various bits (flags, powers of 2) to a certain meaning and apply binary operations to deduce if they are set, then operate on these.

我还要补充一点,二进制运算是接近的想法到计算机的基本电子元件。想像一下,如果该位字段对应于各种电路的输出(通电电流或不是)。通过使用所说的电路的组合不够,你让...一台电脑。

I should also add that binary operations are close in idea to the underlying electronics of a computer. Imagine if the bit fields corresponded to the output of various circuits (carrying current or not). By using enough combinations of said circuits, you make... a computer.

这篇关于C / C ++位阵列或位向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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