用C ++声明一点 [英] Declare a bit in C++

查看:66
本文介绍了用C ++声明一点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个对内存成本有很高要求的程序,我想在不损失性能的情况下节省内存.所以我想将只有两种情况的每个变量都更改为位.

I write a program with highly memory cost requirement and I want save memory with no performance lost. So I want change every variable which has only two situations into bit.

但是我找不到C ++中的位类型,而STL中的 bitset 始终是32位计算机中4字节的倍数.编写数据结构来管理位会导致性能下降.

But I can't find bit type in C++ and bitset in STL is always multiples of 4 byte in 32-bit machine. Writing a data struct to manage bits will cause performance lost.

有什么办法可以声明位值,就像 bit a; 一样?

Is there any way to declare a bit value just like bit a;?

谢谢大家.最后我想要的答案是:你不能用C ++购买一半字节."

Thanks everyone. At last the answer I want is:"you can't buy half bytes in C++".

推荐答案

没有.最小的可寻址实体是一个字节.这是char或unsigned char类型.(最好的类型是整数,因为它与处理器的宽度对齐,因此获取和处理速度最快)

There is none. The smallest addressable entity is a byte. This is the char or unsigned char type. (The best type is the integer because it is aligned to the width of your processor and thus fastest to fetch and work on)

要使用位,您需要使用布尔运算符并以较大的类型屏蔽/移位数据.或使用STL位集.

To work with bits you need to use boolean operators and mask/shift your data in the larger types. Or work with STL bitsets.

这篇关于用C ++声明一点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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