简体   繁体   中英

How to use an int as an array of ints/bools?

I noticed while making a program that a lot of my int type variables never went above ten. I figure that because an int is 2 bytes at the shortest (1 if you count char ), so I should be able to store 4 unsigned ints with a max value of 15 in a short int, and I know I can access each one individually using >> and << :

short unsigned int SLWD = 11434;
S is (SLWD >> 12), L is ((SLWD << 4) >> 12),
W is ((SLWD << 8) >> 12), and D is ((SLWD << 8) >> 12)

However, I have no idea how to encompase this in a function of class, since any type of GetVal() function would have to be of type int , which defeats the purpose of isolating the bits in the first place.

First, remember the Rules of Optimization . But this is possible in C or C++ using bitfields:

struct mystruct {
    unsigned int smallint1 : 3; /* 3 bits wide, values  0 -- 7 */
    signed int   smallint2 : 4; /* 4 bits wide, values -8 -- 7 */
    unsigned int boolean   : 1; /* 1 bit  wide, values  0 -- 1 */
};

It's worth noting that while you gain by not requiring so much storage, you lose because it becomes more costly to access everything, since each read or write now has a bunch of bit twiddling mechanics associated with it. Given that storage is cheap, it's probably not worth it.

Edit: You can also use vector<bool> to store 1-bit bools; but beware of it because it doesn't act like a normal vector ! In particular, it doesn't provide iterators. It's sufficiently different that it's fair to say a vector<bool> is not actually a vector . Scott Meyers wrote very clearly on this topic in 'Effective STL'.

In C, and for the sole purpose of saving space, you can reinterpret the unsigned short as a structure with bitfields (or use such structure without messing with reinterpretations):

#include <stdio.h>

typedef struct bf_
{
   unsigned  x : 4;
   unsigned  y : 4;
   unsigned  z : 4;
   unsigned  w : 4;
} bf;


int main(void)
{
   unsigned short i = 5;
   bf *bitfields = (bf *) &i;

   bitfields->w = 12;
   printf("%d\n", bitfields->x);
   // etc..

   return 0;
}

That's a very common technique. You usually allocate an array of the larger primitive type (eg, ints or longs), and have some abstraction to deal with the mapping. If you're using an OO language, it's usually a good idea to actually define some sort of BitArray or SmartArray or something like that, and impement a getVal() that takes an index. The important thing is to make sure you hide the details of the internal representation (eg, for when you move between platforms).

That being said, most mainstream languages already have this functionality available.
If you just want bits, WikiPedia has a good list. If you want more than bits, you can still find something, or implement it yourself with a similar interface. Take a look at the Java BitSet for reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM