Why bool take 1 byte?

V

Virtual_X

Why bool type take 1 byte and as we know it must have only true or
false (1 or 0) that mean that it need only one bit not the a whole byte
 
U

user923005

Why bool type take 1 byte and as we know it must have only true or
false (1 or 0) that mean that it need only one bit not the a whole byte

Because a byte is the smallest addressible unit in the language.

Consider:

bool foo;
bool *pfoo = &foo;
If a bool consists of a single bit, now what?

Of course, you can make your own bit sets very easily if you like (to
conserve space).
 
J

Juha Nieminen

user923005 said:
Because a byte is the smallest addressible unit in the language.

I'm not sure it's a limitation in the *language* as much as it's a
hardware limitation. Basically in all existing hardware a byte is the
smallest addressable memory amount.
 
J

Juha Nieminen

Virtual_X said:
Why bool type take 1 byte and as we know it must have only true or
false (1 or 0) that mean that it need only one bit not the a whole byte

You can make bool take 1 bit for example if you have a bunch of them
eg. in a struct, like this:

struct A
{
bool a:1, b:1, c:1, d:1, e:1;
};

Each one of those will only need one bit of space, and the compiler
will pack all of those bools into the same byte.
 
A

andreyvul

Because a byte is the smallest addressible unit in the language.

Consider:

bool foo;
bool *pfoo = &foo;
If a bool consists of a single bit, now what?

Of course, you can make your own bit sets very easily if you like (to
conserve space).

use std::bitset for fixed-size bitarray and
std::vector<bool> for variable-size bitarray
however, vector<bool> is pretty much a deprecated hack; if you can,
use bitset
 
V

Virtual_X

You can make bool take 1 bit for example if you have a bunch of them
eg. in a struct, like this:

struct A
{
bool a:1, b:1, c:1, d:1, e:1;

};

Each one of those will only need one bit of space, and the compiler
will pack all of those bools into the same byte.

thank's all
 
J

Jack Klein

I'm not sure it's a limitation in the *language* as much as it's a
hardware limitation. Basically in all existing hardware a byte is the
smallest addressable memory amount.

While that might be true of all the architectures that you are
familiar with, that is most certainly false in general.

There have been and still are many architectures that could read and
write individual bits.

There are still a large number of 8051 based derivatives around, even
though the original vendor no longer makes them. And the Zilog Z80
and its descendants can do the same. Although none come to mind
off-hand, I'm sure there were and are others.

The memory/register containing modifiable individual bits could also
be read or written in larger units, always 8-bit bytes, 16-bit words
in the case of the Z80, but these CPUs have single instructions that
read and write single bits, without changing the others in the unit.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
A

Alf P. Steinbach

* Virtual_X:
Why bool type take 1 byte and as we know it must have only true or
false (1 or 0) that mean that it need only one bit not the a whole byte

A 'bool' is not guaranteed to have size 1 (1 byte).

Depending on the implementation it might have size 2, 4 or 8.

The standard allows any size for 'bool', so theoretically a 'bool' could
have size 4 GBytes, although that compiler would probably not sell very
well.

In the other direction, in C++ a 'bool' that is not a bitfield cannot
have size less than 1 (byte) because it must have an address, and the
smallest address increment is 1.

However, as a bitfield it can be just 1 bit if you want,

struct X
{
bool a : 1; // 1 bit
bool b : 2; // 2 bits
bool c : 5; // 5 bits
};

With at least one compiler this structure fits in one byte, that is, the
total size is 1 (byte). But note that you cannot apply sizeof to a
bitfield. From the sizeof perspective bitfields don't have sizes
because the sizeof granularity (resolution) is one byte, and C++ does
not offer any means of measuring sizes in terms of bits.

Cheers, & hth.,

- Alf
 
R

red floyd

user923005 said:
Because a byte is the smallest addressible unit in the language.

Consider:

bool foo;
bool *pfoo = &foo;
If a bool consists of a single bit, now what?

Of course, you can make your own bit sets very easily if you like (to
conserve space).

Well, I guess on a TI 34010 or 34020 you could do that. But since
sizeof(char) is defined as 1, you can't have anything smaller than a char.
 
R

red floyd

Jack said:
While that might be true of all the architectures that you are
familiar with, that is most certainly false in general.

There have been and still are many architectures that could read and
write individual bits.

There are still a large number of 8051 based derivatives around, even
though the original vendor no longer makes them. And the Zilog Z80
and its descendants can do the same. Although none come to mind
off-hand, I'm sure there were and are others.

No, the Z80 was byte addressable, though it had bit manipulation
instructions.

The TI 34010 and 34020 were true bit-addressible architectures.
However, the Standard defines sizeof(char) as 1, so the language will
not allow you to have anything smaller than a char.
 
V

Virtual_X

No, the Z80 was byte addressable, though it had bit manipulation
instructions.

The TI 34010 and 34020 were true bit-addressible architectures.
However, the Standard defines sizeof(char) as 1, so the language will
not allow you to have anything smaller than a char.

thank's again , i think bitfield is the best choice to save memory but
Is it standard or it will not work in some machines(architecture
dependent)
 
G

Guest

thank's again , i think bitfield is the best choice to save memory but
Is it standard or it will not work in some machines(architecture
dependent)

They are standard, though there are some things about them that are
implementation-defined: allocation, alignment, and sign (i.e. in

struct A
{
int bf : 4;
};

the sign of bf is implementation dependent, so you should always add
"signed" or "unsigned" to be sure).
 
A

Alf P. Steinbach

* Erik Wikström:
[bitfields] are standard, though there are some things about them that are
implementation-defined: allocation, alignment, and sign (i.e. in

struct A
{
int bf : 4;
};

the sign of bf is implementation dependent, so you should always add
"signed" or "unsigned" to be sure).

Learned something new. It's amazing how many quirks, deviations from
general rules, there are in the language. It's the language of quirks.

Cheers,

- Alf
 
T

terminator

You can make bool take 1 bit for example if you have a bunch of them
eg. in a struct, like this:

struct A
{
bool a:1, b:1, c:1, d:1, e:1;

};

Each one of those will only need one bit of space, and the compiler
will pack all of those bools into the same byte.

std::bitset is provided to obtain similar behavior.

regards,
FM.
 
T

terminator

They are standard, though there are some things about them that are
implementation-defined: allocation, alignment, and sign (i.e. in

struct A
{
int bf : 4;
};

the sign of bf is implementation dependent, so you should always add
"signed" or "unsigned" to be sure).

I expected you to recomend something more standard (say std::bitset
for example)?!?!

regards,
FM.
 
G

Guest

I expected you to recomend something more standard (say std::bitset
for example)?!?!

Yes, I would recommend std::bitset, but that would not answer the
question. :)
 
J

Jack Klein

No, the Z80 was byte addressable, though it had bit manipulation
instructions.

The fact that a Z80 actually had to read or read/modify/write an
entire 8-byte byte to test, set, or clear a single bit was completely
immaterial from an architectural point of view, that's a hardware
implementation detail.

Would you claim that a Pentium was not "really" byte addressable,
because it will read a minimum of 64 bits from memory to change the
value of just one of them?

Given:

char ca [8] = { 0 };
++ca[4];

....the processor hardware will execute at least two 64-bit bus cycles,
reading the entire contents of the array if it is suitably aligned to
get at ca[4]. If it winds up in the cache, it will eventually write
all 64 bits back to memory, even though only one 8-bit octet is
changed.

set 0,(hl)

....on a Z80 was a single, and atomic, instruction, regardless of what
the hardware did to make it happen.
The TI 34010 and 34020 were true bit-addressible architectures.
However, the Standard defines sizeof(char) as 1, so the language will
not allow you to have anything smaller than a char.

True, but compilers for every bit-addressable CPU/DSP I am aware of
provide non-standard extensions to access these features.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
J

Joel Yliluoma

You can make bool take 1 bit for example if you have a bunch of them
eg. in a struct, like this:

struct A
{
bool a:1, b:1, c:1, d:1, e:1;
};

Each one of those will only need one bit of space, and the compiler
will pack all of those bools into the same byte.

However, do note that you cannot take the address of the bool
defined that way, nor pass it as a reference, i.e.
A tmp;
std::swap(A.a, A.c);
will fail at compile-time.
 
J

Juha Nieminen

Yannick said:
So I'll turn a question back to you: what advantage would you gain out
of using less than 1 byte for a bool?

If you have one bool it doesn't matter. If you have one million bools,
it can matter.
 
D

drrngrvy

However, do note that you cannot take the address of the bool
defined that way, nor pass it as a reference, i.e.
A tmp;
std::swap(A.a, A.c);
will fail at compile-time.

Obviously you mean
A tmp;
std::swap(tmp.a, tmp.c);
?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,197
Messages
2,571,040
Members
47,634
Latest member
RonnyBoelk

Latest Threads

Top