473,396 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Question Variable size

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

Jul 7 '07 #1
7 1657
"Virtual_X" <C.*******@gmail.comwrote in message
news:11**********************@g13g2000hsf.googlegr oups.com...
>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

Jul 7 '07 #2
On Jul 6, 6:44 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Virtual_X" <C.BsM....@gmail.comwrote in message

news:11**********************@g13g2000hsf.googlegr oups.com...
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
thank's
but why you say 2 i mean in 2 ^ 15 and in 2 ^ 31

Jul 7 '07 #3
On Jul 7, 4:24 am, Virtual_X <C.BsM....@gmail.comwrote:
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
Because a bit has 2 values - 0 or 1.

Jul 8 '07 #4
"Virtual_X" <C.*******@gmail.comwrote in message
news:11**********************@r34g2000hsd.googlegr oups.com...
On Jul 6, 6:44 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
>"Virtual_X" <C.BsM....@gmail.comwrote in message

news:11**********************@g13g2000hsf.googleg roups.com...
>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

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
Jul 8 '07 #5
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. :-)

Jul 10 '07 #6

"Huck Phin" <ch*********@gmail.comwrote in message
news:11**********************@p39g2000hse.googlegr oups.com...
>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
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. :-)

Jul 11 '07 #7
On 2007-07-11 04:24:05 -0400, "Jim Langston" <ta*******@rocketmail.comsaid:
>
"Huck Phin" <ch*********@gmail.comwrote in message
news:11**********************@p39g2000hse.googlegr oups.com...
>>
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
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.

--
-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jul 11 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Jon Slaughter | last post by:
I have a class that is basicaly duplicated throughout several files with only members names changing according to the class name yet with virtually the exact same coding going on. e.g. class...
6
by: Herrcho | last post by:
in K&R Chapter 6.3 it mentions two methods to calculate NKEYS. and points out the first one which is to terminate the list of initializers with a null pointer, then loop along keytab until the...
9
by: Alfonso Morra | last post by:
Hi, I am having some probs with copying memory blocks around (part of a messaging library) and I would like some confirmation to make sure that I'm going about things the right way. I have...
8
by: Fernando Barsoba | last post by:
Hi, I decided to start a new topic about my memory allocation question. One of the answers was that variable length arrays are possible using C99. I found this topic that also mentions it. ...
4
by: emma middlebrook | last post by:
Hi Straight to the point - I don't understand why System.Array derives from IList (given the methods/properties actually on IList). When designing an interface you specify a contract. Deriving...
2
by: Bastian Voigt | last post by:
Hallo List! I just found this old posting on google. Now my question is how can I read an integer value from the PGresult using the binary format? Can someone plz gimme a code example? (please...
1
by: liuhaoran | last post by:
HI. i have a question about memory error. when i change double variable to float variable ,for example: int curGen = 0; double sum = 0; // m_iPopSize is int variable ,NewPop is a vector...
3
by: Chris Roth | last post by:
I'm using VS.net 7.1 on Windows XP. I have a class that hold a container of doubles. One function (foo) for the class calls a sub-function (bar) for each of the doubles. I'd like to...
20
by: fniles | last post by:
I am using VS2003 and connecting to MS Access database. When using a connection pooling (every time I open the OLEDBCONNECTION I use the exact matching connection string), 1. how can I know how...
10
by: Zero | last post by:
Hello all, I wonder about void? To which category in the C programming language does it belong? Of how many bits consits void? Is it possible to define a varibale called void a;
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.