473,566 Members | 3,342 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

1's complement and 2's complement

Hi all,
I have a few doubts in the 1's and 2's complement
representation. Generally negative numbers can be represented using
either 1's complement or 2's complement representation.

1's complement ---reverse all the bits
2's complement ---reverse all the bits + 1

i.e 1's complement of 2 ( 0000 0010 ) is -2 ( 1111 1101 )
But when a number and its complement are added the result must be a
zero right ??
But in this case 0000 0010 + 1111 1101 = 1111 1111 ==[ ?? ]
Should'nt we be getting a zero as result ???

2's complement of 2 ( 0000 0010 ) is -2 ( 1111 1110 )
Adding we get , 0000 0010 + 1111 1110 = 0000 0000 ==[ OK]

Does this complement representation have anything to do with the C's ~
[1's complement] operator ?
Is this representation architecture dependent or compiler dependent ?

Please clarify,

Regards,
Sarathy

Aug 1 '06 #1
20 45497
"sarathy" <sp*********@gm ail.comwrote:
# Hi all,
# I have a few doubts in the 1's and 2's complement
# representation. Generally negative numbers can be represented using
# either 1's complement or 2's complement representation.
#
# 1's complement ---reverse all the bits
# 2's complement ---reverse all the bits + 1
#
# i.e 1's complement of 2 ( 0000 0010 ) is -2 ( 1111 1101 )
# But when a number and its complement are added the result must be a
# zero right ??
# But in this case 0000 0010 + 1111 1101 = 1111 1111 ==[ ?? ]

On a ones complement machine, ~0 is 0, called a negative zero.
Some CPUs convert -0 to +0, some don't. -0 = +0, but also
sometimes -0 < +0.

# Does this complement representation have anything to do with the C's ~
# [1's complement] operator ?

On ones complement CPUs, -x = ~x. Whether this was signficant when C
was first created, you would have to ask Ritchie.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
So....that would make Bethany part black?
Aug 1 '06 #2
sarathy wrote:
Hi all,
I have a few doubts in the 1's and 2's complement
representation. Generally negative numbers can be represented using
either 1's complement or 2's complement representation.

1's complement ---reverse all the bits
2's complement ---reverse all the bits + 1

i.e 1's complement of 2 ( 0000 0010 ) is -2 ( 1111 1101 )
But when a number and its complement are added the result must be a
zero right ??
But in this case 0000 0010 + 1111 1101 = 1111 1111 ==[ ?? ]
Should'nt we be getting a zero as result ???
In a pure 1's complement notation, you have the concept of "minus zero",
which is the ones complement of 0.

So your result is "minus zero".
Aug 1 '06 #3
In article <11************ **********@h48g 2000cwc.googleg roups.com>,
"sarathy" <sp*********@gm ail.comwrote:
Hi all,
I have a few doubts in the 1's and 2's complement
representation. Generally negative numbers can be represented using
either 1's complement or 2's complement representation.

1's complement ---reverse all the bits
2's complement ---reverse all the bits + 1

i.e 1's complement of 2 ( 0000 0010 ) is -2 ( 1111 1101 )
But when a number and its complement are added the result must be a
zero right ??
But in this case 0000 0010 + 1111 1101 = 1111 1111 ==[ ?? ]
Should'nt we be getting a zero as result ???
You did. In 1's complement, there is no unique representation for zero.
All 0's and all 1's are both equal to zero.
Does this complement representation have anything to do with the C's ~
[1's complement] operator ?
Not really
Is this representation architecture dependent or compiler dependent ?
Whether you are doing 1's complement or 2's complement math depends on the
underlying hardware. That being said, I haven't seen a 1's complement
machine in a couple of eons. It's pretty much an obsolete concept as far
as hardware design goes.
Aug 1 '06 #4
Roy Smith wrote:
In article <11************ **********@h48g 2000cwc.googleg roups.com>,
"sarathy" <sp*********@gm ail.comwrote:
1's complement ---reverse all the bits
2's complement ---reverse all the bits + 1

i.e 1's complement of 2 ( 0000 0010 ) is -2 ( 1111 1101 )
But when a number and its complement are added the result must be a
zero right ??
But in this case 0000 0010 + 1111 1101 = 1111 1111 ==[ ?? ]
Should'nt we be getting a zero as result ???

You did. In 1's complement, there is no unique representation for zero.
All 0's and all 1's are both equal to zero.
No, in 8-bit ones complement, zero is represented as either
0x00 or 0x80. 0xff is -127.

The problem is that addition with one's complement is
not the same as addition with 2's complement. To
add two numbers, you have to perform different operations
depending on the signedness of the numbers, and that
is why 2's complement is preferred.

Aug 1 '06 #5
Bill Pursell schrieb:
Roy Smith wrote:
>>In article <11************ **********@h48g 2000cwc.googleg roups.com>,
"sarathy" <sp*********@gm ail.comwrote:
>>>1's complement ---reverse all the bits
2's complement ---reverse all the bits + 1

i.e 1's complement of 2 ( 0000 0010 ) is -2 ( 1111 1101 )
But when a number and its complement are added the result must be a
zero right ??
But in this case 0000 0010 + 1111 1101 = 1111 1111 ==[ ?? ]
Should'nt we be getting a zero as result ???

You did. In 1's complement, there is no unique representation for zero.
All 0's and all 1's are both equal to zero.

No, in 8-bit ones complement, zero is represented as either
0x00 or 0x80. 0xff is -127.
8-bit ones complement? You mean sign and magnitude.

There is only one kind of ones complement for C.

C99, 62.6.2#2: "
— the corresponding value with sign bit 0 is negated (sign and magnitude);
— the sign bit has the value -(2N) (two’s complement);
— the sign bit has the value -(2N - 1) (one’s complement).
"
The problem is that addition with one's complement is
not the same as addition with 2's complement. To
add two numbers, you have to perform different operations
depending on the signedness of the numbers, and that
is why 2's complement is preferred.
And one's complement and sign-magnitude have the advantage
of symmetric value range and others. There have been enough
threads on this.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Aug 1 '06 #6
Hi,
I guess -0 ==1111 1111 is correct in 1's complement notation.
-0 ==1000 0000 is in signed magnitude notation.

Please verify and revert back in case.

Rgrds,
Sarathy

Bill Pursell wrote:
Roy Smith wrote:
In article <11************ **********@h48g 2000cwc.googleg roups.com>,
"sarathy" <sp*********@gm ail.comwrote:
1's complement ---reverse all the bits
2's complement ---reverse all the bits + 1
>
i.e 1's complement of 2 ( 0000 0010 ) is -2 ( 1111 1101 )
But when a number and its complement are added the result must be a
zero right ??
But in this case 0000 0010 + 1111 1101 = 1111 1111 ==[ ?? ]
Should'nt we be getting a zero as result ???
You did. In 1's complement, there is no unique representation for zero.
All 0's and all 1's are both equal to zero.

No, in 8-bit ones complement, zero is represented as either
0x00 or 0x80. 0xff is -127.

The problem is that addition with one's complement is
not the same as addition with 2's complement. To
add two numbers, you have to perform different operations
depending on the signedness of the numbers, and that
is why 2's complement is preferred.
Aug 1 '06 #7

Michael Mair wrote:
Bill Pursell schrieb:
Roy Smith wrote:
>In article <11************ **********@h48g 2000cwc.googleg roups.com>,
"sarathy" <sp*********@gm ail.comwrote:
>>1's complement ---reverse all the bits
2's complement ---reverse all the bits + 1

i.e 1's complement of 2 ( 0000 0010 ) is -2 ( 1111 1101 )
But when a number and its complement are added the result must be a
zero right ??
But in this case 0000 0010 + 1111 1101 = 1111 1111 ==[ ?? ]
Should'nt we be getting a zero as result ???

You did. In 1's complement, there is no unique representation for zero.
All 0's and all 1's are both equal to zero.
No, in 8-bit ones complement, zero is represented as either
0x00 or 0x80. 0xff is -127.

8-bit ones complement? You mean sign and magnitude.
Oops. Of course.
of symmetric value range and others. There have been enough
threads on this.
Agreed!!

--
Bill

Aug 1 '06 #8
sarathy posted:
Please verify and revert back in case.

*Cringe*

I'd love to bludgeon to death the next person I hear utter that phrase.

--

Frederick Gotham
Aug 1 '06 #9
Frederick Gotham said:
sarathy posted:
>Please verify and revert back in case.


*Cringe*

I'd love to bludgeon to death the next person I hear utter that phrase.
Are you sure about that? Please verify and revert back in case.

(And now if you'll excuse me, I have a plane to catch. Or a starship. Or
something... TAXI!)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Aug 1 '06 #10

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

Similar topics

4
52427
by: Abby | last post by:
I have an array which contain Hex no. in each position. For examples, unsigned char data; data = 0x00; data = 0x01; data = 0x02; data = 0xE; data = 0xEF; --> This is the checksum value
8
14366
by: Mantorok Redgormor | last post by:
From least to greatest is it sign magnitude ones complement two's complement Where sign magnitude is the least way to represent integers and two's complement is the best way to represent integers? What are the pitfalls of them?
33
7678
by: Daniel Fadlun | last post by:
Is there a bigger, mathematical, data type in C than the double (64 bit) one or the old long double (80 bit)? I'd like to add precision to my mathematical application, but I can't figure out how. Thanks alot.
7
2109
by: Greenhorn | last post by:
Hi, Is two's complement always used as a storage method or is it computed while computing the expression involved. e.g., int a = -2, b = 3, c = 4, d; d = b - c; Here, is 'a' stored as two's complement of '2'? or is '-c' (two's complement of c) computed on the fly and the resulting value is added to b ( b + (-c))?
22
979
by: sarathy | last post by:
Hi all, I have a few doubts in the 1's and 2's complement representation. Generally negative numbers can be represented using either 1's complement or 2's complement representation. 1's complement ---reverse all the bits 2's complement ---reverse all the bits + 1 i.e 1's complement of 2 ( 0000 0010 ) is -2 ( 1111 1101 ) But when a...
6
4678
by: subramanian | last post by:
Suppose I have the following statement: unsigned char x = 0; If I do, printf("%u\", ~x); it prints the value of UINT_MAX. I am using Intel machine. This same result is printed in both VC++ and gcc.
14
10501
by: darthghandi | last post by:
What would be the most efficient way to calculate the two's complement of a variable length byte array? Thanks for your time.
3
7555
by: vijaybaskar3108 | last post by:
hi, I just want to know how to find complements for a number. These are the following answers for complements 2's complement(10110)=01010 4's complement(1230)=2110 5's complement(4322)=0123 please tell me how to do it.how this answers comes.This is a global edge question. please also tell me how to find 8's complement(7436) please alos...
6
3391
by: Dan Henry | last post by:
I need a sanity check. The following is an exchange on comp.arch.embedded with CBFalconer in a rather long MISRA related thread. Since my little section of that thread and area of interest was never really about MISRA, but rather the one's complement representation of integer constant 0 and now how to zero memory portably, I am bringing the...
0
8109
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7953
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6263
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5485
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2085
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
926
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.