Velocity Reviews > size and range of int in c

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?

Seebs
Guest
Posts: n/a

 11-16-2009
On 2009-11-16, cfaqs c <(E-Mail Removed)> 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 / http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!

cfaqs c
Guest
Posts: n/a

 11-16-2009
On Nov 16, 12:22*pm, Seebs <(E-Mail Removed)> wrote:
> On 2009-11-16, cfaqs c <(E-Mail Removed)> 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 / (E-Mail Removed)://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

Seebs
Guest
Posts: n/a

 11-16-2009
On 2009-11-16, cfaqs c <(E-Mail Removed)> 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 / (E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!

spinoza1111
Guest
Posts: n/a

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

spinoza1111
Guest
Posts: n/a

 11-16-2009
On Nov 16, 3:40*pm, Seebs <(E-Mail Removed)> wrote:
> On 2009-11-16, cfaqs c <(E-Mail Removed)> 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 / (E-Mail Removed)://www.seebs.net/log/<-- lawsuits, religion, and funny pictureshttp://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!

Richard Tobin
Guest
Posts: n/a

 11-16-2009
In article <(E-Mail Removed)>,
cfaqs c <(E-Mail Removed)> 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.

Aatu Koskensilta
Guest
Posts: n/a

 11-16-2009
spinoza1111 <(E-Mail Removed)> writes:

> On Nov 16, 3:40*pm, Seebs <(E-Mail Removed)> 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 ((E-Mail Removed))

"Wovon man nicht sprechen kann, darüber muss man schweigen"
- Ludwig Wittgenstein, Tractatus Logico-Philosophicus

spinoza1111
Guest
Posts: n/a

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

spinoza1111
Guest
Posts: n/a

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

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