 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Thomas Stegen Guest
|
Posted: Tue Jan 25, 2005 2:34 pm Post subject: Re: size of a byte and int? |
|
|
Neo wrote:
| Quote: | "Hans-Bernhard Broeker" <broeker (AT) physik (DOT) rwth-aachen.de> wrote in message
news:35n0smF4paikkU1 (AT) news (DOT) dfncis.de...
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?
|
Because 3 bytes is also capable of representing all those values.
The standard defines a set of allowable sizes. That is all sizes
that can represent at least the above range of values.
Also (though this is not normative, just recommended practice)
int is intended to be the natural size for a given platform,
that means that for example a 32 bit machine will often have
sizeof(int) == 4 even though CHAR_BIT == 8.
IOW, the standard imposes a lower limit and does require that
the actual values are as close as possible to these limits.
--
Thomas.
|
|
| Back to top |
|
 |
Michael Mair Guest
|
Posted: Tue Jan 25, 2005 2:46 pm Post subject: Re: size of a byte and int? |
|
|
Neo wrote:
| Quote: | "Hans-Bernhard Broeker" <broeker (AT) physik (DOT) rwth-aachen.de> wrote in message
news:35n0smF4paikkU1 (AT) news (DOT) dfncis.de...
[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?
|
No. It could be.
| Quote: | As per the post by infobart :
|
Please give us some more information. Some people's newsserver may
not yet have that post, so a better reference is needed.
| Quote: |
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?
|
Parse correctly: _at_least_ versus _minimum_multiple_larger_than_
We only have
sizeof(int)*CHAR_BIT >= 16.
This does not disallow sizeof(int)*CHAR_BIT = 39.
In some cases, you may have a 64 bit machine and then you want to be
able to use the 64 bits, so an implementation might give you 64 bit
longs, 32 bit ints, 16 bit shorts, and 8 bit chars which makes much
more sense than the minimum requirements which leave you without a
64 bit integer data type (if we do not have C99's long long).
The limits are only the minimum requirements to enable you to write
portable programs.
| Quote: | sizeof(long) == 5
sizeof(float) == 7
sizeof(double) == 9
just for the perverse fun of it.
|
:-)
Cheers
Michael
--
E-Mail: Mine is a gmx dot de address.
|
|
| Back to top |
|
 |
Michael Mair Guest
|
Posted: Tue Jan 25, 2005 3:02 pm Post subject: Re: size of a byte and int? |
|
|
Thomas Stegen wrote:
| Quote: | Neo wrote:
"Hans-Bernhard Broeker" <broeker (AT) physik (DOT) rwth-aachen.de> wrote in
message news:35n0smF4paikkU1 (AT) news (DOT) dfncis.de...
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?
Because 3 bytes is also capable of representing all those values.
The standard defines a set of allowable sizes. That is all sizes
that can represent at least the above range of values.
Also (though this is not normative, just recommended practice)
int is intended to be the natural size for a given platform,
that means that for example a 32 bit machine will often have
sizeof(int) == 4 even though CHAR_BIT == 8.
IOW, the standard imposes a lower limit and does require that
not
the actual values are as close as possible to these limits.
|
--
E-Mail: Mine is a gmx dot de address.
|
|
| Back to top |
|
 |
Thomas Stegen Guest
|
Posted: Tue Jan 25, 2005 3:06 pm Post subject: Re: size of a byte and int? |
|
|
Michael Mair wrote:
| Quote: |
Thomas Stegen wrote:
IOW, the standard imposes a lower limit and does require that
not
the actual values are as close as possible to these limits.
|
Oops, yeah, thanks.
--
Thomas.
|
|
| Back to top |
|
 |
Grant Edwards Guest
|
Posted: Tue Jan 25, 2005 3:36 pm Post subject: Re: size of a byte and int? |
|
|
On 2005-01-25, 42Bastian Schick <bastian42 (AT) yahoo (DOT) com> 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).
|
Nope. sizeof (char) is 1. It doesn't matter if the char has
8, 16, or 32 bits.
--
Grant Edwards grante Yow! does your DRESSING
at ROOM have enough ASPARAGUS?
visi.com
|
|
| Back to top |
|
 |
Grant Edwards Guest
|
Posted: Tue Jan 25, 2005 3:38 pm Post subject: Re: size of a byte and int? |
|
|
On 2005-01-25, 42Bastian Schick <bastian42 (AT) yahoo (DOT) com> wrote:
| Quote: | On Tue, 25 Jan 2005 13:11:04 +0530, "Neo"
[email]timeless_illusion (AT) yahoo (DOT) com[/email]> wrote:
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.
|
Common sense is often wrong. As yours seems to be in this
case. I've worked on architectures where a byte (a-la "C") was
32 bits.
| Quote: | That's it, they speak of words avoiding the term byte.
|
The OP is asking about C. In C, 'byte' has a very specific
definition.
--
Grant Edwards grante Yow! Yow! I like my new
at DENTIST...
visi.com
|
|
| Back to top |
|
 |
Grant Edwards Guest
|
Posted: Tue Jan 25, 2005 3:41 pm Post subject: Re: size of a byte and int? |
|
|
On 2005-01-25, Hans-Bernhard Broeker <broeker (AT) physik (DOT) rwth-aachen.de> 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.
|
I disagree. I imagine that these days it's mostly in embedded
work where C programmers run across bytes that aren't 8-bits
wide.
| Quote: | In the context of comp.arch.embedded, your question doesn't
make much sense at all.
|
Yes it does. The only current architectures I'm aware of that
have non-8-bit bytes are used in embedded systems.
--
Grant Edwards grante Yow! Is this TERMINAL fun?
at
visi.com
|
|
| Back to top |
|
 |
Mark A. Odell Guest
|
Posted: Tue Jan 25, 2005 5:52 pm Post subject: Re: size of a byte and int? |
|
|
[email]bastian42 (AT) yahoo (DOT) com[/email] (42Bastian Schick) wrote in
news:41f61067.102320849 (AT) news (DOT) individual.de:
| 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, but maybe you're thinking of CHAR_BITS?
--
- Mark ->
--
|
|
| Back to top |
|
 |
Mark A. Odell Guest
|
Posted: Tue Jan 25, 2005 5:56 pm Post subject: Re: size of a byte and int? |
|
|
[email]bastian42 (AT) yahoo (DOT) com[/email] (42Bastian Schick) wrote in
news:41f610df.102440762 (AT) news (DOT) individual.de:
| Quote: | On Tue, 25 Jan 2005 13:11:04 +0530, "Neo"
[email]timeless_illusion (AT) yahoo (DOT) com[/email]> wrote:
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.
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.
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.
|
Leading underscores are for the implementation, not application programs.
--
- Mark ->
--
|
|
| Back to top |
|
 |
infobahn Guest
|
Posted: Tue Jan 25, 2005 7:08 pm Post subject: Re: size of a byte and int? |
|
|
"Mark A. Odell" wrote:
| Quote: |
[email]bastian42 (AT) yahoo (DOT) com[/email] (42Bastian Schick) wrote in
news:41f61067.102320849 (AT) news (DOT) individual.de:
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, but maybe you're thinking of CHAR_BITS?
|
No, but maybe he's thinking of CHAR_BIT.
|
|
| Back to top |
|
 |
Keith Thompson Guest
|
Posted: Tue Jan 25, 2005 8:16 pm Post subject: Re: size of a byte and int? |
|
|
Hans-Bernhard Broeker <broeker (AT) physik (DOT) rwth-aachen.de> writes:
[...]
| Quote: | 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.
|
Quibble: it's CHAR_BIT, not CHAR_BITS.
Given CHAR_BIT == 13, the minimum allowable sizes are:
sizeof(short) >= 2 /* 26 bits */
sizeof(int) >= 2 /* 26 bits */
sizeof(long) >= 3 /* 39 bits */
short and int must have at least 16 bits that aren't padding bits;
long must have at least 32 non-padding bits. int must be at least as
wide as short, and long must be at least as wide as int, but given
padding bits width and size aren't necessarily directly related. An
even more perverse implementation could have:
sizeof(short) == 3
sizeof(int) == 2
I can't imagine any good reason to do this, but the standard allows
it.
--
Keith Thompson (The_Other_Keith) [email]kst-u (AT) mib (DOT) org[/email] <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
|
|
| Back to top |
|
 |
Jonathan Kirwan Guest
|
Posted: Tue Jan 25, 2005 9:26 pm Post subject: Re: size of a byte and int? |
|
|
On 25 Jan 2005 15:38:47 GMT, Grant Edwards <grante (AT) visi (DOT) com> wrote:
| Quote: | Common sense is often wrong. As yours seems to be in this
case. I've worked on architectures where a byte (a-la "C") was
32 bits.
|
To up this, think PDP-10. 36 bits, unless using very special pointers to packed
structures of five 7-bit ASCII, with a spare bit in the word. But that doesn't
fit too well with C.
Jon
|
|
| Back to top |
|
 |
toby Guest
|
Posted: Tue Jan 25, 2005 9:36 pm Post subject: Re: size of a byte and int? |
|
|
David wrote:
| Quote: | ... In the case of the TMS320F24x, sizeof(char) = sizeof(int), with
both being 16-bit. Makes the chip a real pain.
|
The only inconvenience I can think of is dealing with "packed"
character arrays.
T
|
|
| Back to top |
|
 |
Roberto Waltman Guest
|
Posted: Tue Jan 25, 2005 10:04 pm Post subject: Re: size of a byte and int? |
|
|
<jkirwan (AT) easystreet (DOT) com> wrote:
| Quote: | grante (AT) visi (DOT) com> wrote:
Common sense is often wrong. As yours seems to be in this
case. I've worked on architectures where a byte (a-la "C") was
32 bits.
To up this, think PDP-10. 36 bits, unless using very special pointers to packed
structures of five 7-bit ASCII, with a spare bit in the word. But that doesn't
fit too well with C.
|
( Or 4 9-bit characters. Doesn't fit the "all bytes are flat and 8
bits" theory either...)
As other pointed out, many DSPs can access memory only one word at a
time, and the word width is not necessarily a multiple of 8 bits.
Plus:
* CDC-6600 mainframes: 60 bit words. You could pack 10 6-bit bytes in
each word, but they were not directly addressable. (6 bit was enough
for a capital-letters-only reduced character set. FORTRAN and Pascal
did not need more.)
* Early HP-1000 mini-computers, 16 bit words, only word addressable.
(I believe the same applies to early Nova minis)
* PDP-8 minis (and Intersil 6100 copies) - 12 bit words, only word
addressable.
I once ported the original Small-C compiler to an HP-2113 mini. (May
not be the right model number.) This one was an "advanced model", it
had a couple of index registers and could address individual bytes.
(No stack pointer, of course)
To keep it compatible with existing software, it used the original
instruction set with 15 bit addresses for a 32 Kword address space,
while a few new instruction took 16 bit addresses for byte operations.
In other words, the word at address N contained the two bytes at
addresses (N*2) and (N*2)+1.
A first attempt to make the compiler use byte addressing was soon
discarded. Each memory address or pointer had to be shifted right one
bit when accessing words or left untouched for byte access. At the end
I made the chars 16 bit wide and used the word addresses consistently
across the system.
Was there ever a C compiler for CDC-6600/7??? machines?
Roberto Waltman.
[ Please reply to the group,
return address is invalid ]
|
|
| Back to top |
|
 |
Dave Hansen Guest
|
Posted: Tue Jan 25, 2005 10:40 pm Post subject: Re: size of a byte and int? |
|
|
On Tue, 25 Jan 2005 21:26:27 GMT, Jonathan Kirwan
<jkirwan (AT) easystreet (DOT) com> wrote:
| Quote: | On 25 Jan 2005 15:38:47 GMT, Grant Edwards <grante (AT) visi (DOT) com> wrote:
Common sense is often wrong. As yours seems to be in this
case. I've worked on architectures where a byte (a-la "C") was
32 bits.
To up this, think PDP-10. 36 bits, unless using very special pointers to packed
structures of five 7-bit ASCII, with a spare bit in the word. But that doesn't
fit too well with C.
|
Wouldn't work. A byte (in C) must be at least 8 bits wide.
I used to work on a Univac 1100/80, which also had a 36-bit word.
Each word held six 6-bit (FIELDATA) characters, or four 9-bit
(quarter-word) ASCII characters. The latter would work for C, though
pointer arithemetic might be, um, painful.
FWIW, the Univac never had a C compiler when I was working with it.
FORTRAN 77, Pascal, PL/I, COBOL, SNOBOL, Lisp, GPSS, Prolog, and many
others I'm forgetting now, but no C.
Regards,
-=Dave
--
Change is inevitable, progress is not.
|
|
| Back to top |
|
 |
Powered by phpBB © 2001, 2006 phpBB Group
|