ç黑客用于存储位,用于需要1位空间? [英] C hack for storing a bit that takes 1 bit space?

查看:133
本文介绍了ç黑客用于存储位,用于需要1位空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数字介于0和67600.现在我要来存储它们使用数组是67600元长很长的名单。元件被设置为1,如果一个数是在该组和它被设置为0,如果数是不在集合。 IE浏览器。每次我需要存储数presence只有1位信息。有没有在C / C ++的黑客工具,可以帮助我实现这一目标?

I have a long list of numbers between 0 and 67600. Now I want to store them using an array that is 67600 elements long. An element is set to 1 if a number was in the set and it is set to 0 if the number is not in the set. ie. each time I need only 1bit information for storing the presence of a number. Is there any hack in C/C++ that helps me achieve this?

推荐答案

在C ++中,你可以使用的std ::矢量<布尔> 如果大小是动态的(这是一个的std ::矢量,看到的这个),否则有和std :: bitset (preFER 和std :: bitset 如果可能的话)。还有的boost ::来,dynamic_bitset 如果您需要设置/在运行时改变大小。你可以找到的信息在这里 ,这是pretty爽!

In C++ you can use std::vector<bool> if the size is dynamic (it's a special case of std::vector, see this) otherwise there is std::bitset (prefer std::bitset if possible.) There is also boost::dynamic_bitset if you need to set/change the size at runtime. You can find info on it here, it is pretty cool!

在C(和C ++),您可以手动位运算符实现这一点。常见操作的一个很好的总结是这里。有一件事我想提的是它采用的无符号整数,当你正在做的位操作是个好主意。 &LT;&LT; &GT;&GT; 转向负整数时,是不确定的。您将需要分配一些整型数组像 uint32_t的。如果你想存储 N 位,将采取 N / 32 这些 uint32_t的秒。位 I 保存在 I%32 次出现在的第i位/ 32 '个 uint32_t的。您可能需要使用您的具体架构等方面的限制不同尺寸的整型。 注意:使用现有的实现(例如,作为在C ++中第一段所述,搜索谷歌对于C方案)在滚动自己(除非您特别想preFER,在这种情况下,我建议解决此之前更多地了解二进制/位操作从其他地方。)这种事情已经做了死刑,有好的解决方案。

In C (and C++) you can manually implement this with bitwise operators. A good summary of common operations is here. One thing I want to mention is its a good idea to use unsigned integers when you are doing bit operations. << and >> are undefined when shifting negative integers. You will need to allocate arrays of some integral type like uint32_t. If you want to store N bits, it will take N/32 of these uint32_ts. Bit i is stored in the i % 32'th bit of the i / 32'th uint32_t. You may want to use a differently sized integral type depending on your architecture and other constraints. Note: prefer using an existing implementation (e.g. as described in the first paragraph for C++, search Google for C solutions) over rolling your own (unless you specifically want to, in which case I suggest learning more about binary/bit manipulation from elsewhere before tackling this.) This kind of thing has been done to death and there are "good" solutions.

有一些技巧,会的也许的只消耗一个比特:例如位域的阵列(适用于C作为好),但无论更少的空间被使用达编译器。请参见此链接

There are a number of tricks that will maybe only consume one bit: e.g. arrays of bitfields (applicable in C as well), but whether less space gets used is up to compiler. See this link.

请注意,不管你做什么,你几乎肯定会永远无法使用究竟 N位来存储信息的N位 - 你的电脑很可能无法分配不少于8位:如果你要你要浪费1位7位,如果你想你9将不得不采取16位,浪费其中7。即使您的电脑(CPU + RAM等)可以在单个位操作,如果你在一个操作系统与运行的malloc / 它不会是明智的为您分配跟踪数据,以如此小的precision由于开销。这最后的资格是pretty傻了 - 你不会找到使用的架构,使您可以在同一时间我想象不到8位操作:)

Please note that whatever you do, you will almost surely never be able to use exactly N bits to store N bits of information - your computer very likely can't allocate less than 8 bits: if you want 7 bits you'll have to waste 1 bit, and if you want 9 you will have to take 16 bits and waste 7 of them. Even if your computer (CPU + RAM etc.) could "operate" on single bits, if you're running in an OS with malloc/new it would not be sane for your allocator to track data to such a small precision due to overhead. That last qualification was pretty silly - you won't find an architecture in use that allows you to operate on less than 8 bits at a time I imagine :)

这篇关于ç黑客用于存储位,用于需要1位空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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