Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > size and range of int in c

Reply
Thread Tools

size and range of int in c

 
 
cfaqs c
Guest
Posts: n/a
 
      11-16-2009
The range of int is (2 to the power of 8 - 1) to (2 to the power of
, which requires just 1 byte.
Whereas the size of an integer is 4 bytes.... using sizeof(int).

Why then are 3 extra bytes?


 
Reply With Quote
 
 
 
 
Seebs
Guest
Posts: n/a
 
      11-16-2009
On 2009-11-16, cfaqs c <> wrote:
> The range of int is (2 to the power of 8 - 1) to (2 to the power of
> , which requires just 1 byte.


No it isn't. The range of int is no less than (-32767,32767)...

> Whereas the size of an integer is 4 bytes.... using sizeof(int).


Not necessariily.

> Why then are 3 extra bytes?


There aren't.

If your machine has 4-byte ints, the chances are pretty good that the
range of int on your system is a bit over +/- 2 billion (2^31).

-s
--
Copyright 2009, all wrongs reversed. Peter Seebach / usenet-
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
Reply With Quote
 
 
 
 
cfaqs c
Guest
Posts: n/a
 
      11-16-2009
On Nov 16, 12:22*pm, Seebs <usenet-nos...@seebs.net> wrote:
> On 2009-11-16, cfaqs c <faqs...@gmail.com> wrote:
>
> > The range of int is (2 to the power of 8 *- 1) to (2 to the power of
> > , which requires just 1 byte.

>
> No it isn't. *The range of int is no less than (-32767,32767)...
>
> > Whereas the size of an integer is 4 bytes.... using sizeof(int).

>
> Not necessariily.
>
> > Why then are 3 extra bytes?

>
> There aren't.
>
> If your machine has 4-byte ints, the chances are pretty good that the
> range of int on your system is a bit over +/- 2 billion (2^31).
>
> -s
> --
> Copyright 2009, all wrongs reversed. *Peter Seebach / usenet-nos...@seebs.nethttp://www.seebs.net/log/<-- lawsuits, religion, and funny pictureshttp://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!


If the size is 4 bytes, how is a small number, say 6, stored in the
machine. 6 in binary is 110. So all the prefix bits in the 4 bytes of
storage is padded with zero is it?

Also, how is a negative number stored? I mean i want to know it's
binary representation
 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      11-16-2009
On 2009-11-16, cfaqs c <> wrote:
> If the size is 4 bytes, how is a small number, say 6, stored in the
> machine. 6 in binary is 110. So all the prefix bits in the 4 bytes of
> storage is padded with zero is it?


It's not padding, it's part of the representation of the number. But yes,
it's all 4 bytes. You can't change the size on the fly. (There are
languages in which you can, but that's sort of different.)

> Also, how is a negative number stored? I mean i want to know it's
> binary representation


It depends on your system. Different systems do it differently. The
most common systems are called "1s complement", "2s complement", and
"sign/magnitude.

The risk you face here is that you seem to be trying much too hard to
"figure out what really happens" -- if you do that really early on, it's
easy to get stuck on expecting every computer you ever encounter to be just
like the one you learned on, and since that's not likely to be the case,
it can screw you over badly.

Start out by being aware that these answers may change sometimes, and it'll
be less likely to bite you badly later.

-s
--
Copyright 2009, all wrongs reversed. Peter Seebach / usenet-
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
Reply With Quote
 
spinoza1111
Guest
Posts: n/a
 
      11-16-2009
On Nov 16, 3:22*pm, Seebs <usenet-nos...@seebs.net> wrote:
> On 2009-11-16, cfaqs c <faqs...@gmail.com> wrote:
>
> > The range of int is (2 to the power of 8 *- 1) to (2 to the power of
> > , which requires just 1 byte.

>
> No it isn't. *The range of int is no less than (-32767,32767)...


Yes, which is just as bad as old Visual Basic. In VB 3.0 you had to
code around "integers" restricted to this range and strings whose
length was similarly restricted.

So C is a usable language for safe programming how?
>
> > Whereas the size of an integer is 4 bytes.... using sizeof(int).

>
> Not necessariily.


That constitutes a flaw in C. "C integer" does not, in fact, have a
definite meaning, whereas "integer" has a definition in a modern and
safe language such as C Sharp.
>
> > Why then are 3 extra bytes?

>
> There aren't.
>
> If your machine has 4-byte ints, the chances are pretty good that the
> range of int on your system is a bit over +/- 2 billion (2^31).


If he is using twos complement like a normal person the range can be
known: it is -2^31..2^31-1. This isn't in the Standard. It should be.
>
> -s
> --
> Copyright 2009, all wrongs reversed. *Peter Seebach / usenet-nos...@seebs.nethttp://www.seebs.net/log/<-- lawsuits, religion, and funny pictureshttp://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!


 
Reply With Quote
 
spinoza1111
Guest
Posts: n/a
 
      11-16-2009
On Nov 16, 3:40*pm, Seebs <usenet-nos...@seebs.net> wrote:
> On 2009-11-16, cfaqs c <faqs...@gmail.com> wrote:
>
> > If the size is 4 bytes, how is a small number, say 6, stored in the
> > machine. 6 in binary is 110. So all the prefix bits in the 4 bytes of
> > storage is padded with zero is it?

>
> It's not padding, it's part of the representation of the number. *But yes,
> it's all 4 bytes. *You can't change the size on the fly. *(There are
> languages in which you can, but that's sort of different.)
>
> > Also, how is a negative number stored? I mean i want to know it's
> > binary representation

>
> It depends on your system. *Different systems do it differently. *The
> most common systems are called "1s complement", "2s complement", and
> "sign/magnitude.
>
> The risk you face here is that you seem to be trying much too hard to
> "figure out what really happens" -- if you do that really early on, it's


He needs to know constructively (as in Mathematical intuitionism) what
really happens and may be trusted like a normal person to generalize
his experience UNLESS he is socialised to do things by rote &
shibboleth.

> easy to get stuck on expecting every computer you ever encounter to be just
> like the one you learned on, and since that's not likely to be the case,


Actually, programmers who aren't socialized by corporate fear can
intuit that the computer, like a geometrical diagram, has essential
and accidental features, for the same reason intelligent students in
geometry class don't worry about the thickness of the lines they have
drawn using straightedge and compass in geometrical construction.

The problem, of course, is two fold.

First of all, students no longer are taught geometry axiomatically
since their teachers generally are more trained in paperwork and fear
than in axiomatic reasoning. Instead, they are taught to compete for
rewards through rote & shibboleth.

They never get to the point to which they can be led by a competent
teacher at which they see that a sloppy diagram with thick lines is
functionally equivalent to a precise diagram. Quite the opposite, for
teachers who are themselves ignorant of geometry take points off for
sloppiness, neatness being a shibboleth or password just as here,
yapping about main() is a shibboleth.

I knew starting in January 1970, four months prior to the invasion of
Cambodia, that the IBM 1401 was radically different from the IBM 7094.
Stop patronizing the OP.

> it can screw you over badly.
>
> Start out by being aware that these answers may change sometimes, and it'll
> be less likely to bite you badly later.


If you presume to instruct him at all, you yourself need to know some
modal logic, in particular how to distunguish necessity from accident.
You didn't bother to take compsci. I might forgive you for posing as
an authority if you took modal logic, but don't bet on it.

>
> -s
> --
> Copyright 2009, all wrongs reversed. *Peter Seebach / usenet-nos...@seebs.nethttp://www.seebs.net/log/<-- lawsuits, religion, and funny pictureshttp://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!


 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      11-16-2009
In article <c8bd1bd9-1294-45f5-a3a5->,
cfaqs c <> wrote:

>The range of int is (2 to the power of 8 - 1) to (2 to the power of
>, which requires just 1 byte.


That would require just one bit

-- Richard
--
Please remember to mention me / in tapes you leave behind.
 
Reply With Quote
 
Aatu Koskensilta
Guest
Posts: n/a
 
      11-16-2009
spinoza1111 <> writes:

> On Nov 16, 3:40*pm, Seebs <usenet-nos...@seebs.net> wrote:
>
>> The risk you face here is that you seem to be trying much too hard to
>> "figure out what really happens" -- if you do that really early on,
>> it's

>
> He needs to know constructively (as in Mathematical intuitionism) what
> really happens and may be trusted like a normal person to generalize
> his experience UNLESS he is socialised to do things by rote &
> shibboleth.


This allusion to intuitionism is somewhat obscure. Perhaps you can
elaborate on what you have in mind?

--
Aatu Koskensilta ()

"Wovon man nicht sprechen kann, darüber muss man schweigen"
- Ludwig Wittgenstein, Tractatus Logico-Philosophicus
 
Reply With Quote
 
spinoza1111
Guest
Posts: n/a
 
      11-16-2009
On Nov 16, 11:17*pm, Aatu Koskensilta <aatu.koskensi...@uta.fi> wrote:
> spinoza1111 <spinoza1...@yahoo.com> writes:
> > On Nov 16, 3:40*pm, Seebs <usenet-nos...@seebs.net> wrote:

>
> >> The risk you face here is that you seem to be trying much too hard to
> >> "figure out what really happens" -- if you do that really early on,
> >> it's

>
> > He needs to know constructively (as in Mathematical intuitionism) what
> > really happens and may be trusted like a normal person to generalize
> > his experience UNLESS he is socialised to do things by rote &
> > shibboleth.

>
> This allusion to intuitionism is somewhat obscure. Perhaps you can
> elaborate on what you have in mind?


Intuitionism is the insistence that we do math without the excluded
middle and proceed by constructing proofs from what we know. It
refuses to posit entities which we know "must" exist by way of
excluded middle but which we can't construct.

[Some variants of intuitionism go to unwarranted extremes: I read an
article in the Journal of Symbolic Logic in 1972 which seemed to claim
that "big" numbers such as 100 factorial don't exist in the same way
small numbers exist: my response was to write an assembler program for
the old IBM 1401 to calculate the precise value of 100 factorial. It
was actually easy to do so without simulating extra precision because
the 1401 had a "variable length word".]

Applied to programming, intuitionism would insist that a program must
be constructed bug-free before we can say we know that one exists. And
whether the "program" is constructed using a programming language, or
using mathematical symbols as a written intuitionist proof, it is
going to have essences (meaning) and accidents (notation or an actual
automaton which is either a real computer or an abstract, but fully
described, automaton such as a Turing machine).

What Seebach claims implies that in formal automata, the professor
CANNOT draw a Turing machine on the board and explain that this shows
what computers do, since the professor's constructive proof (proof by
construction) is "only" a picture, and it might confuse the students,
who MIGHT think that on the job they will have to program Turing
machines.

Which is nonsense, and grievously insulting to professors, their
students, and education.
>
> --
> Aatu Koskensilta (aatu.koskensi...@uta.fi)
>
> "Wovon man nicht sprechen kann, darüber muss man schweigen"
> *- Ludwig Wittgenstein, Tractatus Logico-Philosophicus


 
Reply With Quote
 
spinoza1111
Guest
Posts: n/a
 
      11-16-2009
On Nov 16, 11:57*pm, spinoza1111 <spinoza1...@yahoo.com> wrote:
> On Nov 16, 11:17*pm, Aatu Koskensilta <aatu.koskensi...@uta.fi> wrote:
>
> > spinoza1111 <spinoza1...@yahoo.com> writes:
> > > On Nov 16, 3:40*pm, Seebs <usenet-nos...@seebs.net> wrote:

>
> > >> The risk you face here is that you seem to be trying much too hard to
> > >> "figure out what really happens" -- if you do that really early on,
> > >> it's

>
> > > He needs to know constructively (as in Mathematical intuitionism) what
> > > really happens and may be trusted like a normal person to generalize
> > > his experience UNLESS he is socialised to do things by rote &
> > > shibboleth.

>
> > This allusion to intuitionism is somewhat obscure. Perhaps you can
> > elaborate on what you have in mind?

>
> Intuitionism is the insistence that we do math without the excluded
> middle and proceed by constructing proofs from what we know. It
> refuses to posit entities which we know "must" exist by way of
> excluded middle but which we can't construct.
>
> [Some variants of intuitionism go to unwarranted extremes: I read an
> article in the Journal of Symbolic Logic in 1972 which seemed to claim
> that "big" numbers such as 100 factorial don't exist in the same way
> small numbers exist: my response was to write an assembler program for
> the old IBM 1401 to calculate the precise value of 100 factorial. It
> was actually easy to do so without simulating extra precision because
> the 1401 had a "variable length word".]
>
> Applied to programming, intuitionism would insist that a program must
> be constructed bug-free before we can say we know that one exists. And
> whether the "program" is constructed using a programming language, or
> using mathematical symbols as a written intuitionist proof, it is
> going to have essences (meaning) and accidents (notation or an actual
> automaton which is either a real computer or an abstract, but fully
> described, automaton such as a Turing machine).
>
> What Seebach claims implies that in formal automata, the professor
> CANNOT draw a Turing machine on the board and explain that this shows
> what computers do, since the professor's constructive proof (proof by
> construction) is "only" a picture, and it might confuse the students,
> who MIGHT think that on the job they will have to program Turing
> machines.
>
> Which is nonsense, and grievously insulting to professors, their
> students, and education.
>
>
>
>
>
> > --
> > Aatu Koskensilta (aatu.koskensi...@uta.fi)

>
> > "Wovon man nicht sprechen kann, darüber muss man schweigen"
> > *- Ludwig Wittgenstein, Tractatus Logico-Philosophicus- Hide quoted text -

>
> - Show quoted text -


It is however true that Dijkstra, who I consider an intuitionist at
heart, didn't like reasoning with images and claimed (EWD375) to have
discovered a generalization of the Pythagorean theorem to oblique
triangles using symbolic reasoning exclusively. Nonetheless, he was an
intuitionist in the moral sense of refusing to acknowledge that a bug-
free program could exist without some sort of correctness
proof...often one from which he could derive the code of the program.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why is int a[0] not allowed, but int* a = new int[0] is? haijin.biz@gmail.com C++ 9 04-17-2007 09:01 AM
Difference between int i, j; and int i; int j; arun C Programming 8 07-31-2006 05:11 AM
int a[10]; int* p=(int*)((&a)+1); But why p isn't equal to ((&a)+1)? aling C++ 8 10-20-2005 02:42 PM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57