0

I'm pretty new the more "technical" side, per se, of computing, so please bear with me if this is a stupid question. I'm missing what is likely an obvious point, but why are flags+bitmasks more memory efficient than say, an equally sized bunch of booleans, considering wouldn't you have to initialize up to 32 integers to fill up the flag?

Are they just faster computationally, or do they also take up less memory (if this is the case, I'm lost).

I was checking these out, but I didn't see my question:

EDIT: @EJP This is what I'm getting at by the "initializing", from that vipan.com. There are 32 instantiations of an integer, taking up (4 bytes * 32) versus the equivalent of 32 booleans for (1 byte * 32):

// Constants to hold bit masks for desired flags
static final int flagAllOff = 0;  //         000...00000000 (empty mask)
static final int flagbit1 = 1;    // 2^^0    000...00000001
static final int flagbit2 = 2;    // 2^^1    000...00000010
static final int flagbit3 = 4;    // 2^^2    000...00000100
static final int flagbit4 = 8;    // 2^^3    000...00001000
static final int flagbit5 = 16;   // 2^^4    000...00010000
static final int flagbit6 = 32;   // 2^^5    000...00100000
static final int flagbit7 = 64;   // 2^^6    000...01000000
static final int flagbit8 = 128;  // 2^^7    000...10000000
//...
static final int flagbit31 = (int) Math.pow(2, 30);   // 2^^30
//...

// Variable to hold the status of all flags
int flags = 0;

EDIT: So in this case, flags is my flag variable. But if I want to say, represent some value in flags, I'm going to do something of the form of flags = flagbit1 | flagbit2 | flagbit3 | ... | flagbit31. In order to set flags to whatever that turns out to be, I had to create 32 integers called flagbit# and this is what I'm asking about.

Community
  • 1
  • 1
Matt
  • 1,500
  • 8
  • 23
  • 38
  • 1
    Because you can get 32 bit flags into one integer. I don't understand the part about 'initialize up to 32 integers to fill up the flag'. – user207421 Jan 21 '15 at 04:46
  • For a static variable, there is one of it in the whole system. For an instance variable, each object has that variable. – user253751 Jan 21 '15 at 04:53
  • possible duplicate of [Static vs Instance Variables: Difference?](http://stackoverflow.com/questions/21204589/static-vs-instance-variables-difference) – user253751 Jan 21 '15 at 04:53
  • Why do you need to hardcode the constants and why don't you just use (1 << n) inline? It is super-fast! In your solution, I wonder how you would address different masks? I guess you would put it into an array so that you can get it by ARRAY[n]. But do you really think that getting it from an array is faster than doing a bit-shift? I guess not! Since memory is much much slower than CPU. Anyway, benchmarking should be done to ensure which solution is faster... – Siu Ching Pong -Asuka Kenji- Jan 21 '15 at 04:54
  • If you're using a VM language like Java, the memory difference between a set and a flags variable is negligible. Use what's most readable, which is usually an `EnumSet`. – chrylis -cautiouslyoptimistic- Jan 21 '15 at 04:57

3 Answers3

0

FIrst of all, you do not need to initialize up to 32 integers to fill up the flag when using bit operations.

What you need is one variable for all your flags. For example, integer variable has a capacity to represent around 32 flags. So your memory efficiency is around 32 bits in this case.

On the other hand, if you use booleans to represent your flags, you will have to initialize as many booleans as a number of flags you want. And each boolean variable is around 32 bits itself. So your memory efficiency is around 32 * 32 bits in this case

Not only bits are better in terms of memory efficiency, but as far as I am concerned, they are quite faster, then an array of booleans.

Ivan
  • 837
  • 6
  • 15
0

From Java Virtual machine specification

The Java Virtual Machine encodes boolean array components using 1 to represent
true and 0 to represent false. Where Java programming language boolean values
are mapped by compilers to values of Java Virtual Machine type int, the compilers
must use the same encoding.

So there is obviously an overhead.

Dungeon Hunter
  • 19,827
  • 13
  • 59
  • 82
-2

Booleans in many languages are often represented internally as a byte (and some times a word).
The artical "Why use flags+bitmasks rather than a series of booleans?" Enums are often integers. But it also represents something that is a semaphore. The integer size tends to be different for machine/language. Often the usefullness of enum is singular state of an item.

If you play with binary math you will find it is very much more effecient than decimal. Processors do ALL there stuff binary. When they integers - they're faking, they convert it to binary (bit). So there is none of the overhead of 'faking' it

terary
  • 940
  • 13
  • 30