-1
\$\begingroup\$

I was reading: https://www.quora.com/Why-is-the-bool-type-typically-8-bits-long

And the answer was:

Because it’s the smallest type that has an individual memory address so that you can take a pointer to it.

But I'm not convinced, it's right that memory has addresses only for bytes but in Assembly we have lb and sv which loads/saves bytes so what's the problem of making book 1 bit only?

\$\endgroup\$
1
4
\$\begingroup\$

There is no such a thing as generic "assembly". There are thousands of different architectures which might or might not have memory/instructions allowing a single bit access. Yet most of them do have byte granularity. Some don't even have this and for this or performance reasons is using even larger types than byte.

Said that, there is no restriction to have an architecture - specific compiler which would optimize bool to 1-bit memory locations if available. But there would be language-specific subtleties - for example - what would sizeof bool return? It is defined as the number of bytes (or chars) the type is occupying. So even the language itself has this granuality baked in.

I am assuming that we are talking about C language here (and similar).

\$\endgroup\$
2
\$\begingroup\$

You can address individual bits with Bit Set/Bit Clear and Bit Test instructions on most MCUs (BSF, BCF, BTFSC on PIC16), (bis, bic, test on MSP430) and some CPUs such as the Z80 (SET, RES, BIT) while RISC CPUs may not have that functionality at the instruction set level.

These instructions require both an address, and a bit number within that address.

But the C language doesn't allow easy means to access that functionality, and languages following in its footsteps usually don't either. So C type languages use the smallest type the language can address for their bool (or char in pure C) which is usually a byte.

There are other languages where you can treat a byte as a register of 8 booleans, and address an individual bit within that array, for example (Ada, for the MSP430)

package MSP is
   pragma Pure;

   type Byte is Array(0 .. 7) of Boolean;
   Pragma Pack(Byte);
   For Byte'Size use 8;

end MSP;

and some (ugly, because it's auto-generated from TI's C headers) code mapping a specific register as either an integer or an array of booleans package MSP.port1 is

   p1in                   : constant unsigned_8;     --  Port 1 Input 
   Pragma Volatile(p1in);
   Pragma Import(Ada, p1in);      -- see ARM C.6 (13)
   For p1in'Address use   16#20#;
   p1in_bits              : constant Byte;           --  Port 1 Input  Bits
   Pragma Volatile(p1in_bits);
   Pragma Import(Ada, p1in_bits);
   For p1in_bits'Address use   16#20#;
   ...

allowing me to test bit 7 of port P1 as

if p1in_bits(7) then
   ...

So. bool is typically 1 byte long because some languages don't make it easy to access individual bits.

\$\endgroup\$
2
  • \$\begingroup\$ This. The PIC is a good example of an architecture that somewhat extensively allows for bit pseudo-addressing \$\endgroup\$ – Reinderien Jan 21 at 18:51
  • \$\begingroup\$ Unfortunately gcc doesn't support PIC, so it's not currently programmable in Ada. This pushes me towards AVR, MSP, ARM (LONG time since I looked at ARM assembly so I don't can't remember if it has bit addressing. Pretty sure ARM-1 didn't) \$\endgroup\$ – user_1818839 Jan 21 at 19:25
2
\$\begingroup\$

The answer given in Quora is actually the answer.

Circuitry is not free. It takes a buttload of circuitry just to read and write in byte chunks. Now add at least 8x as much (remember all the interconnections running past each other too) for every bit in each byte.

Every assembly instruction available and EACH AND EVERY valid address operand that instruction can accept has a huge network of circuitry dedicated to it. And this also means the memory circuitry itself has to be structured so that each bit is also now addressable which is also 8x denser than with bytes. Just think about that.

\$\endgroup\$
2
  • \$\begingroup\$ Sorry but I didn't understand your answer \$\endgroup\$ – albert Jan 21 at 16:49
  • 4
    \$\begingroup\$ If you are a software only guy, then know that every assembly instruction that exists and every combination of address you can enter for that instruction has a bunch of wires and circuitry dedicated to it. It is not like arbitrarily dividing up addresses on a piece of paper. Read up on some basic digital logic circuits then sit back and try to picture how much of that is required to do anything useful. \$\endgroup\$ – DKNguyen Jan 21 at 16:56
1
\$\begingroup\$

what's the problem of making book 1 bit only?

That would be the compiler doing your job.

On a simple imaginary CPU with 1-cycle instructions and 1-cycle SRAM, the fastest option would be whatever word size can be read in one instruction. This is often a byte but not always. Some CPUs can only read 16 or 32 bit words and require an extra instruction to extract a 8 bit value from that. In this case, you might find out that sizeof(bool) is actually 2 or 4 bytes.

Unless you have lots of bools and want to save RAM, or even more important fit an array in cache memory, in this case the extra instructions to twiddle the bits would result in a faster program due to smaller data size resulting in better cache utilization.

But, C doesn't know how to handle bit arrays because the smallest thing you can put in an array is a char... So you get to use a separate type for bitfields. In C++ you can redefine operator[] so this is transparent.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.