C ++:可伸缩数字类-bitset< 1> *或无符号char * [英] C++: a scalable number class - bitset<1>* or unsigned char*

查看:114
本文介绍了C ++:可伸缩数字类-bitset< 1> *或无符号char *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正计划创建一个数字类.目的是保留任意数量的数字,而不必担心太多(例如int或long).但是同时不要使用太多.例如:

I'm planning on creating a number class. The purpose is to hold any amount of numbers without worrying about getting too much (like with int, or long). But at the same time not USING too much. For example:

如果我的数据确实只需要1-10,则不需要int(4个字节),short(2个字节)或char(1个字节).那么为什么要分配这么多呢? 如果我想保存需要大量数据(在这种情况下仅是整数)的数据,例如过去十亿,那么我就不能. 我的目标是创建一个数字类,可以像字符串一样处理此问题,并调整大小以适合数字.但是在我开始之前,我想知道..

If I have data that only really needs 1-10, I don't need a int (4 bytes), a short(2 bytes) or even a char(1 byte). So why allocate so much? If i want to hold data that requires an extremely large amount (only integers in this scenario) like past the billions, I cannot. My goal is to create a number class that can handle this problem like strings do, sizing to fit the number. But before I begin, I was wondering..

bitset< 1>,bitset是允许我在C ++中最小化位的模板类,非常有用,但是效率高吗?bitset< 1>会定义1位,但是我想对它们进行数组? C ++可以分配一个最小的字节,bitset< 1>是否分配一个字节并提供该字节的1位?如果是这样,我宁愿使用无符号字符*创建我的数字类.

bitset<1>, bitset is a template class that allows me to minipulate bits in C++, quite useful, but is it efficient?, bitset<1> would define 1 bit, but do I want to make an array of them? C++ can allocate a byte minimum, does bitset<1> allocate a byte and provide 1 bit OF that byte? if thats the case I'd rather create my number class with unsigned char*'s.

无符号字符或BYTE保留8个字节,0到256之间的任何内容仅需要一个字节,更多则需要2个字节,然后是3个,它将仅在需要时以字节间隔而不是位间隔继续扩展.

unsigned char, or BYTE holds 8 bytes, anything from 0 - 256 would only need one, more would require two, then 3, it would simply keep expanding when needed in byte intervals rather than bit intervals.

您认为哪位更有效率?如果位集实际分配了1位,则这些位将是有效的,但是我感觉这甚至是不可能的.实际上,在32位处理器上分配字节直到4个字节(32位)实际上可能会更有效,因为32位分配是最有效的,因此从那时起我会一次使用4个字节.

Which do you think is MORE efficient?, the bits would be if bitset actually allocated 1 bit, but I have a feeling that it isn't even possible. In fact, it may actually be more efficient to allocate in bytes until 4 bytes, (32 bits), on a 32 bit processor 32 bit allocation is most efficient thus I would use 4 bytes at a time from then on out.

基本上我的问题是,你有什么想法?我应该如何执行该实现,bitset< 1>或无符号字符(或BYTE)?

Basically my question is, what are your thoughts? how should I go about this implementation, bitset<1>, or unsigned char (or BYTE)??

推荐答案

除非您的目标体系结构是DigiComp-1,否则优化位是很愚蠢的.读取单个位总是比读取整数慢-4位并不比8位更有效.

Optimizing for bits is silly unless you're target architecture is a DigiComp-1. Reading individual bits is always slower than reading ints - 4 bits isn't more efficient than 8.

如果要将无符号字符存储为十进制数字,请使用它.这将是最有效的.

Use unsigned char if you want to store it as a decimal number. This will be the most efficient.

或者,您可以只使用 GMP .

这篇关于C ++:可伸缩数字类-bitset&lt; 1&gt; *或无符号char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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