Then you need to ask yourself: Performance or memory efficiency? Is it worth the extra cycles and instructions to put 8 bools in one byte and & 0x bitmask the relevant one?
Sounds like a compiler problem to me. :p
And you may ask yourself: where is my beautiful house? Where is my beautiful wife?
Letting the days go by, let the water hold me down
- Soon to be
- That's me
Talking heads - once in a lifetime
A lot of times using less memory is actually better for performance because the main bottleneck is memory bandwidth or latency.
It's far more often stored in a word, so 32-64 bytes, depending on the target architecture. At least in most languages.
No it isn't. All statically typed languages I know of use a byte. Which languages store it in an entire 32 bits? That would be unnecessarily wasteful.
The 8-bit Intel 8051 family provides a dedicated bit-addressable memory space (addresses 20h-2Fh in internal RAM), giving 128 directly addressable bits. Used them for years. I'd imagine many microcontrollers have bit-width variables.
bit myFlag = 0;
Or even return from a function:
bit isValidInput(unsigned char input) { // Returns true (1) if input is valid, false (0) otherwise return (input >= '0' && input <= '9'); }
Nothing like that in ARM. Even microcontrollers have enough RAM that nobody cares, I guess.
ARM has bit-banding specifically for this. I think it’s limited to M-profile CPUs (e.g. v7-M) but I’ve definitely used this before. It basically creates a 4-byte virtual address for every bit in a region. So the CPU itself can’t “address” a bit but it can access an address backed by only 1 bit of SRAM or registers (this is also useful to atomically access certain bits in registers without needing to use SW atomics).
We could go the other way as well: TI's C2000 microcontroller architecture has no way to access a single byte, let alone a bit. A Boolean is stored in 16-bits on that one.
if wasting a byte or seven matters to you, then then you need to be working in a lower level language.
It's 7 bits....
Pay attention. 🤪
7 bytes! Look at Mr. Moneybags here!
Well when it comes to bytes, you could say I'm a bit of a millionaire myself.
True.
Well storing that would only take half a bit.
Weird how I usually learn more from the humor communities than the serious ones... 😎
pragma(pack) {
int a:1, b:1, ... h:1;
}
IIRC.
3GPP has an interesting way of serialising bools on the wire with ASN.1
NULL OPTIONAL
meaning only the type would be stored if true, otherwise it won't be set at all
Joke’s on you, I always use 64 bit wide unsigned integers to store a 1 and compare to check for value.
I have a solution with a bit fields. Now your bool is 1 byte :
struct Flags {
bool flag0 : 1;
bool flag1 : 1;
bool flag2 : 1;
bool flag3 : 1;
bool flag4 : 1;
bool flag5 : 1;
bool flag6 : 1;
bool flag7 : 1;
};
Or for example:
struct Flags {
bool flag0 : 1;
bool flag1 : 1:
int x_cord : 3;
int y_cord : 3;
};
just like electronic components, they sell the gates by the chip with multiple gates in them because it's cheaper
Programmer Humor
Welcome to Programmer Humor!
This is a place where you can post jokes, memes, humor, etc. related to programming!
For sharing awful code theres also Programming Horror.
Rules
- Keep content in english
- No advertisements
- Posts must be related to programming or programmer topics