 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Neo Guest
|
Posted: Tue Jan 25, 2005 7:41 am Post subject: size of a byte and int? |
|
|
Hi All,
Is that true that size of a byte not necessarily 8-bit?
What the std. says? If that true, then what will the size of an int,
i mean what sizeof(int) should return?
On my machine sizeof(char) == sizeof(int).
TMS320C5402 DSP (with 16-bit word size).
both returns one. So, it holds true.
But my interpretation is :
Size of int,float etc is specified in terms of bytes, not bits...which is a
standard i.e int -> 2 bytes, char -> 1 byte etc... now, actual size of these
depends up on no. of bits in a byte... which is implementation defined. So,
if we say on a machine its defined 1 byte = 8 bits then size(char) = 1byte =
8 bits size(int) = 2 bytes = 16 bits...
but on other machine 1 byte = 16 bits the size(char) = 1byte = 16 bits and
Size(Int) =2 bytes= 32 bits. In no case, it can be same for both char & int.
Please help me where I am missing.
-Neo
|
|
| Back to top |
|
 |
pete Guest
|
Posted: Tue Jan 25, 2005 7:58 am Post subject: Re: size of a byte and int? |
|
|
Neo wrote:
| Quote: |
Hi All,
Is that true that size of a byte not necessarily 8-bit?
|
Width is measured in bits.
Size is measured in bytes.
--
pete
|
|
| Back to top |
|
 |
Neo Guest
|
Posted: Tue Jan 25, 2005 8:11 am Post subject: Re: size of a byte and int? |
|
|
"pete" <pfiland (AT) mindspring (DOT) com> wrote
| Quote: | Neo wrote:
Hi All,
Is that true that size of a byte not necessarily 8-bit?
Width is measured in bits.
Size is measured in bytes.
--
pete
|
O'kay! What does int and char (data types in C) are measured in?
-Neo
|
|
| Back to top |
|
 |
Peter Nilsson Guest
|
Posted: Tue Jan 25, 2005 8:23 am Post subject: Re: size of a byte and int? |
|
|
Neo wrote:
| Quote: |
Is that true that size of a byte not necessarily 8-bit?
|
Yes.
| Quote: | What the std. says?
|
It says that a byte is CHAR_BIT bits in width, where CHAR_BIT >= 8.
| Quote: | If that true, then what will the size of an int,
i mean what sizeof(int) should return?
|
It will be whatever it is on a given implementation.
| Quote: | On my machine sizeof(char) == sizeof(int).
TMS320C5402 DSP (with 16-bit word size).
both returns one. So, it holds true.
But my interpretation is :
Size of int,float etc is specified in terms of bytes, not bits...
|
No. Integers and floats are specified in terms of value limits
and precision. The minimum range that a signed int must be able
to represent is -32767..32767. Mathematics dictate that this
requires at least 16 bits (including a sign bit.) Thus, an
int will require _at least_ as many bytes as is required to
store 16-bits.
| Quote: | Please help me where I am missing.
|
Google for N869 and read the last public draft of the C99 standard.
--
Peter
|
|
| Back to top |
|
 |
infobahn Guest
|
Posted: Tue Jan 25, 2005 9:21 am Post subject: Re: size of a byte and int? |
|
|
Neo wrote:
| Quote: |
"pete" <pfiland (AT) mindspring (DOT) com> wrote in message
news:41F5FC17.1161 (AT) mindspring (DOT) com...
Neo wrote:
Hi All,
Is that true that size of a byte not necessarily 8-bit?
Width is measured in bits.
Size is measured in bytes.
--
pete
O'kay! What does int and char (data types in C) are measured in?
|
Bytes or bits, depending on whether you want size or width.
sizeof(char) is 1, by definition.
A char comprises CHAR_BIT bits. CHAR_BIT is defined in <limits.h>
and its value can vary from one implementation to the next, but
it can be no lower than 8.
sizeof(int) is at least 16 bits wide. Therefore, it must be
at least (16 + CHAR_BIT - 1) / CHAR_BIT bytes in size (ignoring
any remainder).
int must be able to represent all values in the range -32767 to +32767.
|
|
| Back to top |
|
 |
42Bastian Schick Guest
|
Posted: Tue Jan 25, 2005 9:25 am Post subject: Re: size of a byte and int? |
|
|
| Quote: | Bytes or bits, depending on whether you want size or width.
sizeof(char) is 1, by definition.
|
Could be 2 on some DSPs (IIRC TI's).
--
42Bastian
Do not email to [email]bastian42 (AT) yahoo (DOT) com[/email], it's a spam-only account
Use <same-name>@monlynx.de instead !
|
|
| Back to top |
|
 |
42Bastian Schick Guest
|
Posted: Tue Jan 25, 2005 9:31 am Post subject: Re: size of a byte and int? |
|
|
On Tue, 25 Jan 2005 13:11:04 +0530, "Neo"
<timeless_illusion (AT) yahoo (DOT) com> wrote:
| Quote: | Hi All,
Is that true that size of a byte not necessarily 8-bit?
|
I think common sense is that a byte is nowadays 8 bit.
| Quote: | What the std. says? If that true, then what will the size of an int,
|
Don't mix byte with char ! I don't think there is a std defining the
width of a byte.
| Quote: | i mean what sizeof(int) should return?
On my machine sizeof(char) == sizeof(int).
TMS320C5402 DSP (with 16-bit word size).
^^^ |
That's it, they speak of words avoiding the term byte.
A reason, to define __u8,__u16,__u32 etc. (or the like) depending on
the cpu and/or compiler.
--
42Bastian
Do not email to [email]bastian42 (AT) yahoo (DOT) com[/email], it's a spam-only account
Use <same-name>@monlynx.de instead !
|
|
| Back to top |
|
 |
Paul Black Guest
|
Posted: Tue Jan 25, 2005 9:36 am Post subject: Re: size of a byte and int? |
|
|
42Bastian Schick wrote:
| Quote: | Bytes or bits, depending on whether you want size or width.
sizeof(char) is 1, by definition.
Could be 2 on some DSPs (IIRC TI's).
|
No, it can't. By _definition_, sizeof(chat) is 1. This is stated in the
C standard.
--
Paul Black mailto:paul.black (AT) oxsemi (DOT) com
Oxford Semiconductor Ltd http://www.oxsemi.com
25 Milton Park, Abingdon, Tel: +44 (0) 1235 824 909
Oxfordshire. OX14 4SH Fax: +44 (0) 1235 821 141
|
|
| Back to top |
|
 |
Peter Nilsson Guest
|
Posted: Tue Jan 25, 2005 9:50 am Post subject: Re: size of a byte and int? |
|
|
42Bastian Schick wrote:
| Quote: | "Neo"
Hi All,
Is that true that size of a byte not necessarily 8-bit?
I think common sense is that a byte is nowadays 8 bit.
|
Sense, common to what? An 8-bit entity is called an 'octet'
explicitly by many standards and protocols, precisely to avoid
confusion.
| Quote: | What the std. says? If that true, then what will the size of
an int,
Don't mix byte with char ! I don't think there is a std
defining the width of a byte.
|
The standard does define that a byte has an implementation width
which is big enough to hold the representation of a character
from the basic character set. The standard also states that the
size of all three character types is 1 byte.
| Quote: | i mean what sizeof(int) should return?
On my machine sizeof(char) == sizeof(int).
TMS320C5402 DSP (with 16-bit word size).
^^^
That's it, they speak of words avoiding the term byte.
|
Who is 'they'? A C programmer will still speak of bytes.
| Quote: | A reason, to define __u8,__u16,__u32 etc. (or the like)
depending on the cpu and/or compiler.
|
Programs targetting hosted implementations will generally
have little need for such types. Indeed, implementations
on certain architectures may not be able to represent such
precise width types, outside of inefficient emulation.
C99 introduced the intN_t types to cater for programs which
do rely on precise width twos complement integer types,
however programs which make use of them are not strictly
conforming.
--
Peter
|
|
| Back to top |
|
 |
Neo Guest
|
Posted: Tue Jan 25, 2005 10:38 am Post subject: Re: size of a byte and int? |
|
|
"Peter Nilsson" <airia (AT) acay (DOT) com.au> wrote
| Quote: | 42Bastian Schick wrote:
[-snip-]
C99 introduced the intN_t types to cater for programs which
do rely on precise width twos complement integer types,
however programs which make use of them are not strictly
conforming.
--
Peter
Why not conforming? Then why does the std. defines these? |
-Neo
|
|
| Back to top |
|
 |
Thomas Stegen Guest
|
Posted: Tue Jan 25, 2005 10:46 am Post subject: Re: size of a byte and int? |
|
|
Neo wrote:
| Quote: | "Peter Nilsson" <airia (AT) acay (DOT) com.au> wrote in message
news:1106646608.010225.80650 (AT) z14g2000cwz (DOT) googlegroups.com...
C99 introduced the intN_t types to cater for programs which
do rely on precise width twos complement integer types,
however programs which make use of them are not strictly
conforming.
--
Peter
Why not conforming?
|
(They are not _strictly_ conforming)
Because a platform is not required to provide them.
Just in case it cannot support them.
| Quote: | Then why does the std. defines these?
|
Probably to make the interface to this functionality
uniform across the subset of platforms that can support
them. This is not a bad thing since code that really
needs this is unlikely to ever have much use on a
platform which cannot provide this.
--
Thomas.
|
|
| Back to top |
|
 |
David Guest
|
Posted: Tue Jan 25, 2005 10:55 am Post subject: Re: size of a byte and int? |
|
|
On Tue, 25 Jan 2005 09:25:50 +0000, 42Bastian Schick wrote:
| Quote: | Bytes or bits, depending on whether you want size or width.
sizeof(char) is 1, by definition.
Could be 2 on some DSPs (IIRC TI's).
|
No, sizeof(char) is 1 even on DSP's that cannot access smaller than 16-bit
data. In the case of the TMS320F24x, sizeof(char) = sizeof(int), with
both being 16-bit. Makes the chip a real pain.
|
|
| Back to top |
|
 |
Hans-Bernhard Broeker Guest
|
Posted: Tue Jan 25, 2005 1:45 pm Post subject: Re: size of a byte and int? |
|
|
[F'up2 cut down --- should have been done by OP!]
In comp.arch.embedded Neo <timeless_illusion (AT) yahoo (DOT) com> wrote:
| Quote: | Is that true that size of a byte not necessarily 8-bit?
What the std. says? If that true, then what will the size of an int,
i mean what sizeof(int) should return?
|
You cross-posted this question to two newsgroups, one of which (c.a.e)
it is seriously off-topic in. Please don't do that, or if you do, at
least explain why, and set a Followup-To. As is, your posting
silently assumes a context that only applies to half your audience,
causing all kinds of needless confusion.
In the context of comp.arch.embedded, your question doesn't make much
sense at all. In the context of comp.lang.c, the answers to the above
are: Yes. The same. Implementation-defined. (In that order).
Your confusion seems to come from the fact that you don't realize that
sizeof(int), too, is implementation-defined (within limitations set up
by the standard on the range of values an int must at least be able to
hold). In theory, an implementation could have, say
CHAR_BITS == 13
sizeof(short) == 2
sizeof(int) == 3
sizeof(long) == 5
sizeof(float) == 7
sizeof(double) == 9
just for the perverse fun of it.
--
Hans-Bernhard Broeker (broeker (AT) physik (DOT) rwth-aachen.de)
Even if all the snow were burnt, ashes would remain.
|
|
| Back to top |
|
 |
Neo Guest
|
Posted: Tue Jan 25, 2005 2:17 pm Post subject: Re: size of a byte and int? |
|
|
"Hans-Bernhard Broeker" <broeker (AT) physik (DOT) rwth-aachen.de> wrote
| Quote: | [F'up2 cut down --- should have been done by OP!]
In comp.arch.embedded Neo <timeless_illusion (AT) yahoo (DOT) com> wrote:
Is that true that size of a byte not necessarily 8-bit?
What the std. says? If that true, then what will the size of an int,
i mean what sizeof(int) should return?
You cross-posted this question to two newsgroups, one of which (c.a.e)
it is seriously off-topic in. Please don't do that, or if you do, at
least explain why, and set a Followup-To. As is, your posting
silently assumes a context that only applies to half your audience,
causing all kinds of needless confusion.
In the context of comp.arch.embedded, your question doesn't make much
sense at all. In the context of comp.lang.c, the answers to the above
are: Yes. The same. Implementation-defined. (In that order).
Your confusion seems to come from the fact that you don't realize that
sizeof(int), too, is implementation-defined (within limitations set up
by the standard on the range of values an int must at least be able to
hold). In theory, an implementation could have, say
CHAR_BITS == 13
sizeof(short) == 2
sizeof(int) == 3
|
Shouldn't sizeof(int) be 2 here?
As per the post by infobart :
sizeof(int) is at least 16 bits wide. Therefore, it must be
at least (16 + CHAR_BIT - 1) / CHAR_BIT bytes in size (ignoring
any remainder).
int must be able to represent all values in the range -32767 to +32767.
2 bytes here consisting of 26 bits can represent all these values so, it
should be 2 why 3 then?
-Neo
| Quote: | sizeof(long) == 5
sizeof(float) == 7
sizeof(double) == 9
just for the perverse fun of it.
--
Hans-Bernhard Broeker (broeker (AT) physik (DOT) rwth-aachen.de)
Even if all the snow were burnt, ashes would remain.
|
|
|
| Back to top |
|
 |
Ignacio G. T. Guest
|
Posted: Tue Jan 25, 2005 2:31 pm Post subject: Re: size of a byte and int? |
|
|
David wrote:
| Quote: | On Tue, 25 Jan 2005 09:25:50 +0000, 42Bastian Schick wrote:
Bytes or bits, depending on whether you want size or width.
sizeof(char) is 1, by definition.
Could be 2 on some DSPs (IIRC TI's).
No, sizeof(char) is 1 even on DSP's that cannot access smaller than 16-bit
data. In the case of the TMS320F24x, sizeof(char) = sizeof(int), with
both being 16-bit. Makes the chip a real pain.
|
Mmm... yes, but, beware:
sizeof(char) = 1;
sizeof('c') = 1; // In C++
sizeof('c') = sizeof(int); /* In C */
|
|
| Back to top |
|
 |
Powered by phpBB © 2001, 2006 phpBB Group
|