473,412 Members | 2,081 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,412 software developers and data experts.

Why the Range of Int is less by one at positive side?.........

The range of signed int is - 2^15 to + (2^15 )-1 (-32768 to 32767) Why
the one less at positive Range ? (compared to the negative side..!)
I came to know that it uses 2's compliment internally for storing the
negative numbers . why it uses 2's compliment but not the ones compliment
for storing the negative numbers ?
Nov 14 '05 #1
19 2395
Harshan wrote:
The range of signed int is - 2^15 to + (2^15 )-1 (-32768 to 32767) Why
the one less at positive Range ? (compared to the negative side..!)
because we need to represent the number 'zero' as well.
I came to know that it
What is 'it' ? Are you referring to the implementation.

uses 2's compliment internally for storing the
negative numbers . why it uses 2's compliment but not the ones compliment
for storing the negative numbers ?


This is entirely dependent on the machine / implementation.
And ansi C does not have any say on this.

--
Karthik.
Nov 14 '05 #2
harshan,

it is less by one bcos it takes zero into account when
positive numbers are taken care of. i.e. if u want to have
10 smallest non negative numbers it'll b frm 0 -9 not 1-10.
thats why at the positive range its one less than as expected

Nov 14 '05 #3
On Mon, 24 Jan 2005 15:38:29 +0530, Harshan wrote:
The range of signed int is - 2^15 to + (2^15 )-1 (-32768 to 32767) Why
the one less at positive Range ? (compared to the negative side..!)
I came to know that it uses 2's compliment internally for storing the
negative numbers . why it uses 2's compliment but not the ones compliment
for storing the negative numbers ?


The C language permits a 1's complement or sign-magnitude representation
of signed integers in addition to 2's complement. Your compiler uses 2's
complement almost certainly because that is what the processor it is
targetting uses. Modern processors tend to use 2's complement because it
has some nice properties and can simplify the hardware design (e.g. you
can avoid separate instructions for signed and unsigned addition and
subtraction). An obvious issue with 1's complement and sign-magnitude is
2 separate representations for the number zero.

Lawrence

Nov 14 '05 #4
yes
i think this is coorect because of 2 zeros in 1's compliment . they are
taking 2's compliment for representation of -ve numbers and that consumes
one number for representing zero , but the negative side it is not consuming
the number so it will be 8000 to ffff -ve side and 1 to 7fff +ve
side in hex representation .

but i found that if you exceed the range value the compiler will take wrap
the value , it is somthing circular style.

"Lawrence Kirby" <lk****@netactive.co.uk> wrote in message
news:pa****************************@netactive.co.u k...
On Mon, 24 Jan 2005 15:38:29 +0530, Harshan wrote:
The range of signed int is - 2^15 to + (2^15 )-1 (-32768 to 32767) Why the one less at positive Range ? (compared to the negative side..!)
I came to know that it uses 2's compliment internally for storing the
negative numbers . why it uses 2's compliment but not the ones compliment for storing the negative numbers ?


The C language permits a 1's complement or sign-magnitude representation
of signed integers in addition to 2's complement. Your compiler uses 2's
complement almost certainly because that is what the processor it is
targetting uses. Modern processors tend to use 2's complement because it
has some nice properties and can simplify the hardware design (e.g. you
can avoid separate instructions for signed and unsigned addition and
subtraction). An obvious issue with 1's complement and sign-magnitude is
2 separate representations for the number zero.

Lawrence

Nov 14 '05 #5

"Harshan" <ha***********@in.bosch.com> wrote in message
news:ct**********@ns2.fe.internet.bosch.com...
but i found that if you exceed the range value the compiler will take wrap the value , it is somthing circular style.


\The\ compiler does not exist. \Your\ compiler (whatever it is) does.
Probably any HW with 2's complement will result in simular behavior, but in
fact, overflowing a signed integer results in undefined behavior, so do not
count on this behavior. I may be *very* different for another
processor/compiler.

PS. Does anyone know any *modern* processors with anything but 2's
complement encoding of signed integers? I can't remember ever running into
one.
Nov 14 '05 #6

In article <41***********************@dreader4.news.xs4all.nl >, "dandelion" <da*******@meadow.net> writes:

PS. Does anyone know any *modern* processors with anything but 2's
complement encoding of signed integers? I can't remember ever running into
one.


I think some processors still support packed decimal in hardware, but
that's in addition to 2's-complement. Which is good, since p-d isn't
a pure binary representation, so a C implementation on a p-d-only
machine would have to emulate all the integer operations.

--
Michael Wojcik mi************@microfocus.com

Proverbs for Paranoids, 1: You may never get to touch the Master,
but you can tickle his creatures. -- Thomas Pynchon
Nov 14 '05 #7
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Michael Wojcik wrote:
In article <41***********************@dreader4.news.xs4all.nl >, "dandelion" <da*******@meadow.net> writes:
PS. Does anyone know any *modern* processors with anything but 2's
complement encoding of signed integers? I can't remember ever running into
one.

I think some processors still support packed decimal in hardware,


Yes, the zSeries mainframe (of the System 360/370/390 line) still supports
packed decimal in hardware. Most mainframe languages support these native bcd
datatypes directly in the language.
but
that's in addition to 2's-complement.
IIRC, BCD support on IBM mainframes originally was an option; you could order
the original 'basic' system with binary integer support only, a 'scientific'
system with binary integer and floatingpoint support or a 'business' system with
binary integer and BCD support. The "System 360" changed that by incorporating
both the 'business' and 'scientific' support into one hardware architecture.
Legend has it that the system was named "360" because it was an 'all
encompasing' processor, and the name alluded to the 360 degrees of a complete
circle representing the complete circle of basic, business and scientific computing.

Which is good, since p-d isn't
a pure binary representation, so a C implementation on a p-d-only
machine would have to emulate all the integer operations.

- --
Lew Pitcher
IT Consultant, Enterprise Data Systems,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFB9TtEagVFX4UWr64RAuUsAKCT/nW+GjHQaRhHffN4QgzA/K3orQCg8CZU
gC23OnjwVDSsFCQtA5xUay0=
=LHK+
-----END PGP SIGNATURE-----
Nov 14 '05 #8
"Harshan" <ha***********@in.bosch.com> wrote in message
news:ct**********@ns2.fe.internet.bosch.com...
The range of signed int is - 2^15 to + (2^15 )-1 (-32768 to 32767)
It might be for your implementation, but the range required by
the C standard is -32767 through 32767 inclusive (but an implementation
is allowed to provide a larger range.

Why
the one less at positive Range ? (compared to the negative side..!)
Because apparently your implementation takes advantage of its
specific representation for integers.

I came to know that it uses 2's compliment internally for storing the
negative numbers . why it uses 2's compliment but not the ones compliment
for storing the negative numbers ?


Because that's what its authors decided to do. The language
standard allows one's complement, two's complement, or
signed magnitude.
-Mike
Nov 14 '05 #9
Mike Wahler wrote:
"Harshan" <ha***********@in.bosch.com> wrote in message
news:ct**********@ns2.fe.internet.bosch.com...
The range of signed int is - 2^15 to + (2^15 )-1 (-32768 to 32767)

It might be for your implementation, but the range required by
the C standard is -32767 through 32767 inclusive (but an implementation
is allowed to provide a larger range.
Why
the one less at positive Range ? (compared to the negative side..!)

Because apparently your implementation takes advantage of its
specific representation for integers.

I came to know that it uses 2's compliment internally for storing the
negative numbers . why it uses 2's compliment but not the ones compliment
for storing the negative numbers ?

Because that's what its authors decided to do. The language
standard allows one's complement, two's complement, or
signed magnitude.
-Mike

It's what the authors of the CPU's accumulator decided to do.

--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #10
"Harshan" <ha***********@in.bosch.com> writes:
The range of signed int is - 2^15 to + (2^15 )-1 (-32768 to 32767) Why
the one less at positive Range ? (compared to the negative side..!)
Becase 0 (Zero) is also a number ;-), and someone decided Zero to be a
positive number, so we have:
-2^15, 0 (Zero), 2^15 - 1

BTW hwat you showed was the rage of a signed _short_ not a signed int
;-)
I came to know that it uses 2's compliment internally for storing the
negative numbers . why it uses 2's compliment but not the ones compliment
for storing the negative numbers ?


AFAIK there can happen collisions with other numbers if the mashine only
uses ! - complement of a number to represent the negative "compliment"

Kind regads,
Nicolas

--
| Nicolas Pavlidis | Elvis Presly: |\ |__ |
| Student of SE & KM | "Into the goto" | \|__| |
| pa****@sbox.tugraz.at | ICQ #320057056 | |
|-------------------University of Technology, Graz----------------|
Nov 14 '05 #11
"Joe Wright" <jo********@comcast.net> wrote in message
news:0Z********************@comcast.com...
Mike Wahler wrote:
Because that's what its authors decided to do. The language
standard allows one's complement, two's complement, or
signed magnitude.
-Mike

It's what the authors of the CPU's accumulator decided to do.


While it is probably most commonly done, the C language doesn't
require that an implementation use the same representation
as does the CPU.

-Mike
Nov 14 '05 #12
Mike Wahler wrote:
"Joe Wright" <jo********@comcast.net> wrote in message
news:0Z********************@comcast.com...
Mike Wahler wrote:
Because that's what its authors decided to do. The language
standard allows one's complement, two's complement, or
signed magnitude.
-Mike


It's what the authors of the CPU's accumulator decided to do.

While it is probably most commonly done, the C language doesn't
require that an implementation use the same representation
as does the CPU.

-Mike

Respectfully, nonsense.

C allows one's complement, two's complement and signed magnitude only to
accommodate CPU's structured that way. It would be unusally perverse for
an implementation to 'convert' to/from the CPU's natural representation
to another.

Do you know of an implementation which which does?

--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #13
"Joe Wright" <jo********@comcast.net> wrote in message
news:cs********************@comcast.com...
Mike Wahler wrote:
"Joe Wright" <jo********@comcast.net> wrote in message
news:0Z********************@comcast.com...
Mike Wahler wrote:

Because that's what its authors decided to do. The language
standard allows one's complement, two's complement, or
signed magnitude.
-Mike

It's what the authors of the CPU's accumulator decided to do.

While it is probably most commonly done, the C language doesn't
require that an implementation use the same representation
as does the CPU.

-Mike

Respectfully, nonsense.

C allows one's complement, two's complement and signed magnitude only to
accommodate CPU's structured that way. It would be unusally perverse for an
implementation to 'convert' to/from the CPU's natural representation to
another.

Do you know of an implementation which which does?


Depends on your definition of "natural". The IBM Mainframe
supports both two's complement and binary coded decimal (BCD)
integers. There are machine instructions for converting
between the two formats, as well as machine instructions
for performing the usual operations, like comparing,
addition, subtraction, multiplication, division. So, a
C compiler for that platform could be implemented to use
BCD for "natural" integer represention (although it would
be insane for a commercial compiler).
Nov 14 '05 #14
"Joe Wright" <jo********@comcast.net> wrote in message
news:cs********************@comcast.com...
Mike Wahler wrote:
"Joe Wright" <jo********@comcast.net> wrote in message
news:0Z********************@comcast.com...
Mike Wahler wrote:

Because that's what its authors decided to do. The language
standard allows one's complement, two's complement, or
signed magnitude.
-Mike

It's what the authors of the CPU's accumulator decided to do.

While it is probably most commonly done, the C language doesn't
require that an implementation use the same representation
as does the CPU.

-Mike

Respectfully, nonsense.


Really? If what I wrote is 'nonsense', that indicates that
the C standard imposes such a requirement. If you feel that
it does, please cite it.

C allows one's complement, two's complement and signed magnitude only to
accommodate CPU's structured that way. It would be unusally perverse for
an implementation to 'convert' to/from the CPU's natural representation
to another.
The C standard does not prohibit 'perversion'. Just look at
the IOCCC. :-)

Do you know of an implementation which which does?


No I don't, nor was I claiming that any do (nor that none do).
I'm only claiming they're not prohibited from doing so.

Also see 'xarax's reply.

-Mike
Nov 14 '05 #15
Nicolas Pavlidis <pa****@sbox.tugraz.at> writes:
"Harshan" <ha***********@in.bosch.com> writes:
The range of signed int is - 2^15 to + (2^15 )-1 (-32768 to 32767) Why
the one less at positive Range ? (compared to the negative side..!)
Becase 0 (Zero) is also a number ;-), and someone decided Zero to be a
positive number, so we have:
-2^15, 0 (Zero), 2^15 - 1


No, nobody decided that zero is a positive number, because it isn't.
Zero is a non-negative number. In a 2's-complement representation all
non-negative numbers have the sign bit set to 0, and all negative
numbers have the sign bit set to 1.
BTW hwat you showed was the rage of a signed _short_ not a signed int
;-)


That depends on the implementation. Both short and int are required
to have a range of at least -32767 .. +32767; there are plenty of
implementations where both short and int are 16 bits.
I came to know that it uses 2's compliment internally for storing the
negative numbers . why it uses 2's compliment but not the ones compliment
for storing the negative numbers ?


AFAIK there can happen collisions with other numbers if the mashine only
uses ! - complement of a number to represent the negative "compliment"


Both signed-magnitude and 1's-complement representations have two
distinct representations for zero, referred to as -0 and +0. This
probably requires a little extra work (either in hardware or in
software) to keep things consistent; for example, the two
representations must compare equal.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #16
dandelion wrote:
"Harshan" <ha***********@in.bosch.com> wrote in message
news:ct**********@ns2.fe.internet.bosch.com...

but i found that if you exceed the range value the compiler will take
wrap the value , it is somthing circular style.


\The\ compiler does not exist. \Your\ compiler (whatever it is) does.
Probably any HW with 2's complement will result in simular behavior, but in
fact, overflowing a signed integer results in undefined behavior, so do not
count on this behavior. I may be *very* different for another
processor/compiler.

PS. Does anyone know any *modern* processors with anything but 2's
complement encoding of signed integers? I can't remember ever running into
one.


Even if the processor uses 2s complement there is not guarantee it will
wrap. The DSPs from TI that I have programmed have a saturation mode
where overflow results in the maximum value in whichever direction you
are going.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #17

In article <ln****************@newsread3.news.pas.earthlink.n et>, "xarax" <xa***@email.com> writes:
So, a C compiler for that platform could be implemented to use
BCD for "natural" integer represention (although it would
be insane for a commercial compiler).


That would be a non-conforming implementation. The Standard requires
a "pure binary" integer representation. In C90 that's in 6.1.2.5.

A C implementation could offer BCD arithmetic as an extension, of
course.

--
Michael Wojcik mi************@microfocus.com
Nov 14 '05 #18
mw*****@newsguy.com (Michael Wojcik) writes:
A C implementation could offer BCD arithmetic as an extension, of
course.


Or it could do decimal floating point.
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x1f6},*p=
b,x,i=24;for(;p+=!*p;*p/=4)switch(x=*p&3)case 0:{return 0;for(p--;i--;i--)case
2:{i++;if(1)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
Nov 14 '05 #19
Ben Pfaff <bl*@cs.stanford.edu> writes:
mw*****@newsguy.com (Michael Wojcik) writes:
A C implementation could offer BCD arithmetic as an extension, of
course.


Or it could do decimal floating point.


Which is a separate issue. Integer types are required to be pure
binary. Floating-point types (as far as I know) are not.

I think the requirements on floating-point representations are less
stringent than those on integers because there's a greater variety of
floating-point representations in the real world. (The world seems to
be converging on 2's-complement and IEEE floating-point, but of course
C has to support systems that haven't joined the convergence.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #20

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

Similar topics

29
by: Chris Dutrow | last post by:
I searched around on the net for a bit, couldn't find anything though. I would like to find some code for a function where I input A Range Of Integers For example: Function( 1, 100 ); And the...
29
by: Steve R. Hastings | last post by:
When you compile the expression for i in range(1000): pass does Python make an iterator for range(), and then generate the values on the fly? Or does Python actually allocate the list and...
32
by: Chad | last post by:
What is the difference between range and precision in c? Also, if c doesn't support fixed point numbers, how is the addition of two integers possible? Chad
5
by: pcnerd | last post by:
I'm trying to create a program that plots randomly colored pixels on a bitmap & then displays the bitmap. When I run the program, I see the pixels being plotted down the left side of the form. When...
7
by: Joe Van Dyk | last post by:
Hi, Say I have: class Latitude { friend bool operator==(const Latitude& lhs, const Latitude& rhs); friend bool operator<(const Latitude& lhs, const Latitude& rhs); friend std::ostream&...
1
by: Thanigaivel | last post by:
Hi All, i have assigned some large positive int value (32 bit length) X to unsigned int variable and due to the range constraints that variable stores the same positive value X in negative...
8
by: Rahul | last post by:
Hello Everyone, I was wondering what is the datatype of obj? Is it an integer? If so what if the number of enumerations crosses over the limit of an integer (2^32-1)? enum { CLUB, DIAMONDS
63
by: deepak | last post by:
Hi, Can someone give the standard function which can create positive integer value in C? Thanks, Deepak
10
by: Rafael Cunha de Almeida | last post by:
Hi, I've found several sites on google telling me that I shouldn't use rand() % range+1 and I should, instead, use something like: lowest+int(range*rand()/(RAND_MAX + 1.0))
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.