size and range of int in c

Discussion in 'C Programming' started by cfaqs c, Nov 16, 2009.

  1. cfaqs c

    cfaqs c Guest

    The range of int is (2 to the power of 8 - 1) to (2 to the power of
    8), which requires just 1 byte.
    Whereas the size of an integer is 4 bytes.... using sizeof(int).

    Why then are 3 extra bytes?
    cfaqs c, Nov 16, 2009
    #1
    1. Advertising

  2. cfaqs c

    Seebs Guest

    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
    > 8), 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 /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    Seebs, Nov 16, 2009
    #2
    1. Advertising

  3. cfaqs c

    cfaqs c Guest

    On Nov 16, 12:22 pm, Seebs <> wrote:
    > 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
    > > 8), 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 / ://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
    cfaqs c, Nov 16, 2009
    #3
  4. cfaqs c

    Seebs Guest

    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 /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    Seebs, Nov 16, 2009
    #4
  5. cfaqs c

    spinoza1111 Guest

    On Nov 16, 3:22 pm, Seebs <> wrote:
    > 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
    > > 8), 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 / ://www.seebs.net/log/<-- lawsuits, religion, and funny pictureshttp://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    spinoza1111, Nov 16, 2009
    #5
  6. cfaqs c

    spinoza1111 Guest

    On Nov 16, 3:40 pm, Seebs <> wrote:
    > 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


    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 / ://www.seebs.net/log/<-- lawsuits, religion, and funny pictureshttp://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    spinoza1111, Nov 16, 2009
    #6
  7. In article <>,
    cfaqs c <> wrote:

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


    That would require just one bit :)

    -- Richard
    --
    Please remember to mention me / in tapes you leave behind.
    Richard Tobin, Nov 16, 2009
    #7
  8. spinoza1111 <> writes:

    > On Nov 16, 3:40 pm, Seebs <> 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
    Aatu Koskensilta, Nov 16, 2009
    #8
  9. cfaqs c

    spinoza1111 Guest

    On Nov 16, 11:17 pm, Aatu Koskensilta <> wrote:
    > spinoza1111 <> writes:
    > > On Nov 16, 3:40 pm, Seebs <> 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 ()
    >
    > "Wovon man nicht sprechen kann, darüber muss man schweigen"
    >  - Ludwig Wittgenstein, Tractatus Logico-Philosophicus
    spinoza1111, Nov 16, 2009
    #9
  10. cfaqs c

    spinoza1111 Guest

    On Nov 16, 11:57 pm, spinoza1111 <> wrote:
    > On Nov 16, 11:17 pm, Aatu Koskensilta <> wrote:
    >
    > > spinoza1111 <> writes:
    > > > On Nov 16, 3:40 pm, Seebs <> 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 ()

    >
    > > "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.
    spinoza1111, Nov 16, 2009
    #10
  11. cfaqs c

    Eric Sosman Guest

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


    The range you mention has just two values (or possibly
    128), so one bit (seven bits) is enough.

    However (as others have pointed out), the range of `int'
    is wider than you suppose. For example, an `int' can store
    negative values, which your range{,s} exclude{s,}.

    --
    Eric Sosman
    lid
    Eric Sosman, Nov 16, 2009
    #11
  12. cfaqs c

    Nick Guest

    spinoza1111 <> writes:

    > On Nov 16, 3:22 pm, Seebs <> wrote:
    >> 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
    >> > 8), 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?


    In the light of that last sentence I think it's time, for the fourth
    time of asking, to ask "is English your native language".

    Before you return to preaching about the meaning of "clarity" to the
    rest of us, I wonder if you could explain the paragraph above.

    You seem to be suggesting that any machine with finite length integers
    is "bad", Is that it?
    --
    Online waterways route planner: http://canalplan.org.uk
    development version: http://canalplan.eu
    Nick, Nov 16, 2009
    #12
  13. cfaqs c

    John Kelly Guest

    On Mon, 16 Nov 2009 20:49:07 +0000, Nick
    <> wrote:

    >spinoza1111 <> writes:


    >> So C is a usable language for safe programming how?


    >In the light of that last sentence I think it's time, for the fourth
    >time of asking, to ask "is English your native language".


    I don't know the answer to that question. But it's apparent that his
    vocabulary and command of the language are greater than a majority of
    native English speakers.

    What would you conclude if you knew his native language?


    --
    Webmail for Dialup Users
    http://www.isp2dial.com/freeaccounts.html
    John Kelly, Nov 16, 2009
    #13
  14. cfaqs c

    Seebs Guest

    On 2009-11-16, Aatu Koskensilta <> wrote:
    > This allusion to intuitionism is somewhat obscure. Perhaps you can
    > elaborate on what you have in mind?


    It seems unlikely. He is not much good at elaborating or explaining.

    Spinny's just mad that people who learn about the abstract machine rather
    than a real machine seem to be better able to keep code running, and thus
    stay employed. The idea that it might be possible to learn from this
    experience seems foreign.

    -s
    --
    Copyright 2009, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    Seebs, Nov 16, 2009
    #14
  15. cfaqs c

    Seebs Guest

    On 2009-11-16, John Kelly <> wrote:
    > I don't know the answer to that question. But it's apparent that his
    > vocabulary and command of the language are greater than a majority of
    > native English speakers.


    Strictly speaking, probably true, but the mistakes he makes are absolutely
    fascinating.

    -s
    --
    Copyright 2009, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    Seebs, Nov 16, 2009
    #15
  16. cfaqs c

    Richard Bos Guest

    Aatu Koskensilta <> wrote:

    > spinoza1111 <> writes:
    >
    > > On Nov 16, 3:40 pm, Seebs <> 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?


    *Bzzt* Assumption of presence of object without evidence thereof.

    Richard
    Richard Bos, Nov 16, 2009
    #16
  17. In article <> spinoza1111 <> writes:
    ....
    > > 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.


    So they should have instantly obsoleted the Gould computers? Are you
    proposing a situation where a single provider of computers mandates how
    everything is done?

    Darn, a standards mandated monopoly...
    --
    dik t. winter, cwi, science park 123, 1098 xg amsterdam, nederland, +31205924131
    home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
    Dik T. Winter, Nov 17, 2009
    #17
  18. In article <> spinoza1111 <> writes:
    ....
    > 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.


    Interesting that you again bring in something you know nothing about. In
    this case intuitionism. Try once to read the works by Brouwer and
    acknowledge that you are spouting nonsense. Intuitionism is about a way
    to *prove* something, not about generalisations. Most mathematical
    generalisations are not valid in intuitionism because there is no
    constructive proof.

    > 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.


    That is indeed the way trisection proofs are worked out. Sheesh, can you
    not get anything right?
    --
    dik t. winter, cwi, science park 123, 1098 xg amsterdam, nederland, +31205924131
    home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
    Dik T. Winter, Nov 17, 2009
    #18
  19. cfaqs c

    spinoza1111 Guest

    On Nov 17, 5:54 am, Seebs <> wrote:
    > On 2009-11-16, Aatu Koskensilta <> wrote:
    >
    > > This allusion to intuitionism is somewhat obscure. Perhaps you can
    > > elaborate on what you have in mind?

    >
    > It seems unlikely.  He is not much good at elaborating or explaining.
    >
    > Spinny's just mad that people who learn about the abstract machine rather
    > than a real machine seem to be better able to keep code running, and thus
    > stay employed.  The idea that it might be possible to learn from this
    > experience seems foreign.


    Don't confuse me with the anxious fearful you that rages behind the
    mask. My point was that there's no such thing as the "abstract
    machine" that one doesn't see through a glass darkly. The compsci
    student, which you weren't, is equally apt to be deluded by the Turing
    Machine as the DeVry student by the IBM PC, since both models have
    essential and accidental features. You've adopted anti-PCism as a
    fashion statement but don't have the proper background to start viral
    attacks on people such as Schildt.
    >
    > -s
    > --
    > Copyright 2009, all wrongs reversed.  Peter Seebach / ://www.seebs.net/log/<-- lawsuits, religion, and funny pictureshttp://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    spinoza1111, Nov 17, 2009
    #19
  20. cfaqs c

    spinoza1111 Guest

    On Nov 17, 10:35 am, "Dik T. Winter" <> wrote:
    > In article <>spinoza1111<> writes:
    > ...
    >  > > 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..
    >
    > So they should have instantly obsoleted the Gould computers?  Are you
    > proposing a situation where a single provider of computers mandates how
    > everything is done?


    Yes. A tremendous amount of time has been wasted in useless
    "competition" that would have been better spent meeting real human
    needs such as clean water and education.

    >
    > Darn, a standards mandated monopoly...
    > --
    > dik t. winter, cwi, science park 123, 1098 xg amsterdam, nederland, +31205924131
    > home: bovenover 215, 1025 jn  amsterdam, nederland;http://www.cwi.nl/~dik/
    spinoza1111, Nov 17, 2009
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,206
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,626
    Old Wolf
    Jan 20, 2004
  3. arun
    Replies:
    8
    Views:
    450
    Dave Thompson
    Jul 31, 2006
  4. aling
    Replies:
    8
    Views:
    943
    Jim Langston
    Oct 20, 2005
  5. Replies:
    9
    Views:
    429
    James Kanze
    Apr 17, 2007
Loading...

Share This Page