Question Variable size

V

Virtual_X

I am new to c++ and i know that
"short int" size is 2bytes with rang -32768 to
32767
and "int" size is 4bytes with rang -2147483648
to 2147483647

How!!

the "int" range must be -(2 * 32767) to (2 * 32767)

is there is any one can help me in that
 
J

Jim Langston

Virtual_X said:
I am new to c++ and i know that
"short int" size is 2bytes with rang -32768 to
32767
and "int" size is 4bytes with rang -2147483648
to 2147483647

How!!

the "int" range must be -(2 * 32767) to (2 * 32767)

is there is any one can help me in that

First, it is platform dependant. Those are the sizes on your platform.

Second, a 4 byte int can hold a lot more than a 2 byte int, not just 2 times
as much. Exponentionally.

There are 4 * 8 bits in a 4 byte int wich is 32 bits. 1 bit is used for a
sign bit, leaving 31 bits to store the number. The max number is:
2^31 or 2147483648 so it can hold 2147483648 values and a sign bit. Because
of how 2's compliment works, the range for a 4 byte signed bit is
-2147483648 to 2147483647

A 2 byte int has 2 * 8 or 16 bits.
One bit for sign. 2^15 is 32768
 
V

Virtual_X

First, it is platform dependant. Those are the sizes on your platform.

Second, a 4 byte int can hold a lot more than a 2 byte int, not just 2 times
as much. Exponentionally.

There are 4 * 8 bits in a 4 byte int wich is 32 bits. 1 bit is used for a
sign bit, leaving 31 bits to store the number. The max number is:
2^31 or 2147483648 so it can hold 2147483648 values and a sign bit. Because
of how 2's compliment works, the range for a 4 byte signed bit is
-2147483648 to 2147483647

A 2 byte int has 2 * 8 or 16 bits.
One bit for sign. 2^15 is 32768

thank's
but why you say 2 i mean in 2 ^ 15 and in 2 ^ 31
 
J

Jim Langston

Virtual_X said:
thank's
but why you say 2 i mean in 2 ^ 15 and in 2 ^ 31

Because computers are binary. A bit can either be on or off. If it's on,
it's 1. If it's off, it's 2. Take 2 bits. There are 4 combinations:
0 0
0 1
1 0
1 1

Which in decimal are 0, 1, 2 and 3.
3 bits gives us twice as many combinations.
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1

Each added bit will double the number of values it can represent. It is
binary, or base 2.

think about Decimal (the numbering system we know).
With 1 diget we can hold 10 values. with 2 digits we can hold 100. with 3
we can hold 1000. It is 10 ^ places. 10 because there are 10 digits. Base
2 has 2 digits, so it's 2 ^ places. Base 16 would be 16 ^ places.

If you google "introduction to binary" you'll get a few hits. Here is one
that's not that bad: http://atrevida.comprenica.com/atrtut01.html
 
H

Huck Phin

I would like to put the same thing that was said into a little
different terms, and an easy way to figure it, for me, anyways.

As was said before in almost every post before me, 1 byte = 8 bits;
therefore, 2 bytes = 16 bits. As was mentioned before, this
determines how big of a number you can actually store in this data
type. So, if there are only two possibilities in a computer 1 and 0,
then the number of possible combinations you can have is 2 ^ 16 which
equals 65,536.

It stores 1 bit as a positive or negative number, so on the number
line, you would have 0 - +xxx, and -xxx - 0. To find what max values
it can reach you would break it in half. 65,536 / 2 = 32768 max
values in the positive or negative range. If you did not have a 0,
then it's range would be from -32,768 - +32,768, but since we have an
additional 0, 1 is subtracted from the negative spectrum, representing
the full range of values, -32,767 - +32,768.

I know that this has already been said earlier, but in a different
fashion. This is how I think of it in my mind. Sometimes it helps to
get a different way of looking at it to help.

This method will show you how to determine the largest values for any
integer datatype that you work with, regardless of size. Let me show
you. If you want to figure out what the range of values are for a
1,024 bit integer, you would find that it would be 2 ^ 1,024 =
1.79E308 / 2 = -8.98E307 - +8.98E307.

Might come in handy if you ever have to program a spaceship travel
route to the sun, or Halley's comet. :)
 
J

Jim Langston

Huck Phin said:
I would like to put the same thing that was said into a little
different terms, and an easy way to figure it, for me, anyways.

As was said before in almost every post before me, 1 byte = 8 bits;
therefore, 2 bytes = 16 bits. As was mentioned before, this
determines how big of a number you can actually store in this data
type. So, if there are only two possibilities in a computer 1 and 0,
then the number of possible combinations you can have is 2 ^ 16 which
equals 65,536.

It stores 1 bit as a positive or negative number, so on the number
line, you would have 0 - +xxx, and -xxx - 0. To find what max values
it can reach you would break it in half. 65,536 / 2 = 32768 max
values in the positive or negative range. If you did not have a 0,
then it's range would be from -32,768 - +32,768, but since we have an
additional 0, 1 is subtracted from the negative spectrum, representing
the full range of values, -32,767 - +32,768.

0 is actually taken from the positive side. It would be
-32768 - +32767
 
P

Pete Becker

0 is actually taken from the positive side. It would be
-32768 - +32767

It depends on how negative numbers are represented. With 2's complement
you can have a range from -32768 through 32767, inclusive. With
sign-magnitude, there are two zeros (one positive and one negative),
and the values range from -32767 through 32767, inclusive.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,291
Messages
2,571,453
Members
48,137
Latest member
IndiraMcCo

Latest Threads

Top