Why bool take 1 byte? | | |
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 | | | | re: Why bool take 1 byte?
On Oct 31, 4:54 pm, Virtual_X <C.BsM....@gmail.comwrote: Quote:
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). | | | | re: Why bool take 1 byte?
user923005 wrote: Quote:
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. | | | | re: Why bool take 1 byte?
On Oct 31, 8:13 pm, user923005 <dcor...@connx.comwrote: Quote:
On Oct 31, 4:54 pm, Virtual_X <C.BsM....@gmail.comwrote:
> Quote:
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).
use std::bitset for fixed-size bitarray and
std::vector<boolfor variable-size bitarray
however, vector<boolis pretty much a deprecated hack; if you can,
use bitset | | | | re: Why bool take 1 byte?
Virtual_X wrote: Quote:
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. | | | | re: Why bool take 1 byte?
On Oct 31, 4:46 pm, Juha Nieminen <nos...@thanks.invalidwrote: Quote:
Virtual_X wrote: Quote:
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.
thank's all | | | | re: Why bool take 1 byte?
On Thu, 01 Nov 2007 02:43:52 +0200, Juha Nieminen
<nospam@thanks.invalidwrote in comp.lang.c++: Quote:
user923005 wrote: Quote:
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.
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 | | | | re: Why bool take 1 byte?
user923005 wrote: Quote:
On Oct 31, 4:54 pm, Virtual_X <C.BsM....@gmail.comwrote: Quote:
>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).
>
>
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. | | | | re: Why bool take 1 byte?
Jack Klein wrote: Quote:
On Thu, 01 Nov 2007 02:43:52 +0200, Juha Nieminen
<nospam@thanks.invalidwrote in comp.lang.c++:
> Quote:
>user923005 wrote: Quote:
>>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.
>
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. | | | | re: Why bool take 1 byte?
On Oct 31, 9:09 pm, red floyd <no.s...@here.dudewrote: Quote:
Jack Klein wrote: Quote:
On Thu, 01 Nov 2007 02:43:52 +0200, Juha Nieminen
<nos...@thanks.invalidwrote in comp.lang.c++:
> Quote: Quote:
user923005 wrote:
>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.
> Quote:
While that might be true of all the architectures that you are
familiar with, that is most certainly false in general.
> Quote:
There have been and still are many architectures that could read and
write individual bits.
> Quote:
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.
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) | | | | re: Why bool take 1 byte?
On 2007-11-01 13:01, Virtual_X wrote: Quote:
On Oct 31, 9:09 pm, red floyd <no.s...@here.dudewrote: Quote:
>Jack Klein wrote: Quote:
On Thu, 01 Nov 2007 02:43:52 +0200, Juha Nieminen
<nos...@thanks.invalidwrote in comp.lang.c++:
>> Quote:
>user923005 wrote:
>>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.
>> Quote:
While that might be true of all the architectures that you are
familiar with, that is most certainly false in general.
>> Quote:
There have been and still are many architectures that could read and
write individual bits.
>> Quote:
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.
>
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).
--
Erik Wikström | | | | re: Why bool take 1 byte?
On Nov 1, 3:46 am, Juha Nieminen <nos...@thanks.invalidwrote: Quote:
Virtual_X wrote: Quote:
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.
std::bitset is provided to obtain similar behavior.
regards,
FM. | | | | re: Why bool take 1 byte?
On Nov 1, 3:41 pm, Erik Wikström <Erik-wikst...@telia.comwrote: Quote:
On 2007-11-01 13:01, Virtual_X wrote:
>
>
>
>
> Quote:
On Oct 31, 9:09 pm, red floyd <no.s...@here.dudewrote: Quote:
Jack Klein wrote:
On Thu, 01 Nov 2007 02:43:52 +0200, Juha Nieminen
<nos...@thanks.invalidwrote in comp.lang.c++:
> Quote: Quote:
user923005 wrote:
>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.
> Quote: Quote:
While that might be true of all the architectures that you are
familiar with, that is most certainly false in general.
> Quote: Quote:
There have been and still are many architectures that could read and
write individual bits.
> Quote: Quote:
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.
> Quote: Quote:
No, the Z80 was byte addressable, though it had bit manipulation
instructions.
> Quote: Quote:
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.
> Quote:
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).
>
I expected you to recomend something more standard (say std::bitset
for example)?!?!
regards,
FM. | | | | re: Why bool take 1 byte?
On 2007-11-01 16:39, terminator wrote: Quote:
On Nov 1, 3:41 pm, Erik Wikström <Erik-wikst...@telia.comwrote: Quote:
>On 2007-11-01 13:01, Virtual_X wrote:
>>
>>
>>
>>
>> Quote:
On Oct 31, 9:09 pm, red floyd <no.s...@here.dudewrote:
>Jack Klein wrote:
On Thu, 01 Nov 2007 02:43:52 +0200, Juha Nieminen
<nos...@thanks.invalidwrote in comp.lang.c++:
>> Quote:
>user923005 wrote:
>>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.
>> Quote:
While that might be true of all the architectures that you are
familiar with, that is most certainly false in general.
>> Quote:
There have been and still are many architectures that could read and
write individual bits.
>> Quote:
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.
>> Quote:
>No, the Z80 was byte addressable, though it had bit manipulation
>instructions.
>> Quote:
>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.
>> Quote:
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).
>>
>
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. :-)
--
Erik Wikström | | | | re: Why bool take 1 byte?
On Thu, 01 Nov 2007 05:09:05 GMT, red floyd <no.spam@here.dudewrote
in comp.lang.c++: Quote:
Jack Klein wrote: Quote:
On Thu, 01 Nov 2007 02:43:52 +0200, Juha Nieminen
<nospam@thanks.invalidwrote in comp.lang.c++: Quote:
user923005 wrote:
>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.
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 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. Quote:
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 | | | | re: Why bool take 1 byte?
On Thu, 01 Nov 2007 02:46:18 +0200, Juha Nieminen wrote: Quote:
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.
--
Joel Yliluoma - http://bisqwit.iki.fi/
: comprehension = 1 / (2 ^ precision) | | | | re: Why bool take 1 byte?
Yannick Tremblay wrote: Quote:
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. | | | | re: Why bool take 1 byte?
On Nov 2, 1:03 pm, Joel Yliluoma <bisq...@iki.fiwrote: Quote:
On Thu, 01 Nov 2007 02:46:18 +0200, Juha Nieminen wrote: Quote:
You can make bool take 1 bit for example if you have a bunch of them
eg. in a struct, like this:
> Quote:
struct A
{
bool a:1, b:1, c:1, d:1, e:1;
};
> Quote:
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.
Obviously you mean
A tmp;
std::swap(tmp.a, tmp.c);
? | | | | re: Why bool take 1 byte?
On Sat, 03 Nov 2007 01:52:56 -0000, drrngrvy wrote: Quote: Quote:
> A tmp;
> std::swap(A.a, A.c);
>
Obviously you mean
A tmp;
std::swap(tmp.a, tmp.c);
?
Yes, sorry.
--
Joel Yliluoma - http://bisqwit.iki.fi/
: comprehension = 1 / (2 ^ precision) | | | | re: Why bool take 1 byte?
On Nov 1, 7:11 pm, Erik Wikström <Erik-wikst...@telia.comwrote: Quote:
On 2007-11-01 16:39, terminator wrote:
>
>
>
>
> Quote:
On Nov 1, 3:41 pm, Erik Wikström <Erik-wikst...@telia.comwrote: Quote:
On 2007-11-01 13:01, Virtual_X wrote:
> Quote: Quote:
On Oct 31, 9:09 pm, red floyd <no.s...@here.dudewrote:
Jack Klein wrote:
On Thu, 01 Nov 2007 02:43:52 +0200, Juha Nieminen
<nos...@thanks.invalidwrote in comp.lang.c++:
> Quote: Quote:
user923005 wrote:
>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.
> Quote: Quote:
While that might be true of all the architectures that you are
familiar with, that is most certainly false in general.
> Quote: Quote:
There have been and still are many architectures that could read and
write individual bits.
> Quote: Quote:
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.
> Quote: Quote:
No, the Z80 was byte addressable, though it had bit manipulation
instructions.
> Quote: Quote:
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.
> Quote: Quote:
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)
> Quote: Quote:
They are standard, though there are some things about them that are
implementation-defined: allocation, alignment, and sign (i.e. in
> Quote: Quote:
struct A
{
int bf : 4;
};
> Quote: Quote:
the sign of bf is implementation dependent, so you should always add
"signed" or "unsigned" to be sure).
> Quote:
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. :-)
>
--
Erik Wikström- Hide quoted text -
>
- Show quoted text -
oops!! I was driving too fast. | | | | re: Why bool take 1 byte?
On Oct 31, 4:13 pm, user923005 <dcor...@connx.comwrote: Quote:
On Oct 31, 4:54 pm, Virtual_X <C.BsM....@gmail.comwrote:
> Quote:
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).
Sorry for my bluntness. I don't think this is a good example. Because
here pfoo is a pointer to a bool, itself is not a bool. foo could be
one bit but its address can be a byte (In modern OS, the address shall
be 4 bytes or more). I think bool is not 1 bit is because the
compiler fetch data and instruction based on bytes. Even you implement
your bool as 1 bit, the whole address location will be fetched while
the code is running.
Using bitset is one way to use limited memory to do more work such as
sorting. |  | | | | Forums
Visit our community forums for general discussions and latest on Bytes
/bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,576 network members.
|