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))?
Joe 7 2124
On Fri, 26 Aug 2005 02:34:16 -0700, Greenhorn wrote: 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'?
The value -2 is stored in a. On implementations that use 2's complement to
represent negative numbers the appropriate 2's complement representation
will be used. 2's complement isn't required though, 1's complement and
sign-magnitude are also possible. The actual representation used is rarely
important, all you need to know is that the value -2 is stored in a.
or is '-c' (two's complement of c) computed on the fly and the resulting value is added to b ( b + (-c))?
C could be negated and added to b (again there's no need to assume 2's
complement), but if the processor has a subtract instruction (and most do)
it is more likely that that would be used instead. This is an
implementation issue, as long as the correct result is produced the
compiler can generate whatever code it likes.
You seem to be very focussed on the representation used and 2's complement
in particular. C defines arithmetic operations in terms of values, not
representations so don't worry about it.
Lawrence
hi lawrence,
thanks for the reply,
I intended to ask how the machine's which use two's complement deal
with these.
You say that most processor's today have subtract instruction, do they
have a circuit for computing subtraction or do they internally use some
thing like two's complement in evaluating the expression (e.g., b - c)
greene
Greenhorn wrote:
please leave some context in. The original question was about two's
complement arithmetic I intended to ask how the machine's which use two's complement deal with these. You say that most processor's today have subtract instruction, do they have a circuit for computing subtraction or do they internally use some thing like two's complement in evaluating the expression (e.g., b - c)
you are now asking questions about the internals of the hardware. If
my vague recollections about hardware are correct then one reason for
using twos complement is its pretty easy to build a common add/subtract
unit. Ie. it can do both depending on the setting of a flag. Consider
that subtraction is adding a negative. So subtraction is complement and
add.
You may want to think about taking these questions to a computer
architecture group.
--
Nick Keighley
Hi,
yes you are correct that two's complement was used to reduce to the
costs involved in putting another circuit for performing a subtraction
operation. I had to switch to the hardware level as the answer by
'lawrence' says that today there are machines which do support
subtraction , i wasn't sure if he meant that with those machines there
is no need of something like two's complement notation.
greene
"Greenhorn" <te************ @yahoo.com> wrote:
# Hi,
# Is two's complement always used as a storage method or is it computed
# while computing the expression involved.
Within C alone, you can only discern such things when you operate
on the value, for example when you convert to hex (printf("%x",va lue))
or mix integer and bits operations (like (-1)^(~1)). Outside the CPU,
you really cannot tell. However no hardware I've ever heard does this
kind of conversion between the CPU and memory.
# 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))?
Depends on the CPU. Some old CPUs (like PDP-8) didn't have a
subtract instruction. Instead they would zero the accumulator,
add c, complement and increment c, and then add b, then store
in d while clearing the accumulator.
There's also constant folding. An optimising compiler would
see that in this fragment
d = b-c
b = 3
c = 4
d = 3-4
d = -1
and do the entire computation during compilation. All that
might be left is a store of -1 into d.
--
SM Ryan http://www.rawbw.com/~wyrmwif/
A bunch of savages in this town.
"Greenhorn" <te************ @yahoo.com> wrote I intended to ask how the machine's which use two's complement deal with these.
Virtually every machine uses two's complement, and it is reasonably safe to
assume that you have it on the rare occasions when you need to access bit
values directly.
However it is not the only possible method for representing negative
numbers. Floating-point formats, for example, almost always use a flag bit
to represent a negative.
C doesn't make any requirements, though it is implict that the C
representation will also be the underlying hardware representation. You say that most processor's today have subtract instruction, do they have a circuit for computing subtraction or do they internally use some thing like two's complement in evaluating the expression (e.g., b - c)
When you say "most processors" do you mean "the processor which my learner C
program is likely to run on" or do you mean "the randomly-selected processor
from the set of all in existence"? Most processors are not 3 giga hertz
Intel jobs that sit in PCs, but little embedded control devices that keep
fridges at the right temperature or make the dolly cry when her dummy is
removed. C is a major language for programming these chips.
You can get away without a "subtract" instruction by using the procedure
invert, increment, add (discarding overflow) as long as you use two's
complement. So the C expression x = a - b might well resolve to those three
machine language instructions. If you have a dedicated "subtract"
instruction, the complier will usually use it in preference, because the
chip designers include it to speed things up. On a PC, there will always be
a subtract instruction. On a little embedded device, maybe not.
On Fri, 26 Aug 2005 07:33:09 -0700, Greenhorn wrote: Hi, yes you are correct that two's complement was used to reduce to the costs involved in putting another circuit for performing a subtraction operation.
If you have an add instruction then it is about as easy to adapt to to
support subtraction using 1's complement. It is even easier with
sign-magnitude since all you have to do is flip the sign bit of the value
you are subtracting.
The reasons for using 2's complement are different. For example when the
result is represented using the same width as the operands 2's complement
addition and subtraction use the same bit manipulations as unsigned
addition and subtraction, just with different conditions for overflow.
This means that the hardware doesn't have to support different
instructions for signed and unsigned arithmetic.
I had to switch to the hardware level as the answer by 'lawrence' says that today there are machines which do support subtraction , i wasn't sure if he meant that with those machines there is no need of something like two's complement notation.
Numbers have to be represented in one form or another. Compilers
for machines that support a subtraction instruction will generally use
that instead of some sequence of negation and addition, because it is
likely to be more efficient. OTOH the compiler could generate negation and
addition if it wants to, as long as the code works it doesn't matter as
far as C is concerned.
Lawrence This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Ben Finney |
last post by:
Howdy all,
I'm experimenting with carrying my personal computing environment around
on a keychain USB flash storage device. I have the usual suspects on
there: SSH keys, GPG keys, program configs.
I'm probably not the first to think that a standalone distribution of
Python would be a useful thing to have in my back pocket. Finding
information on that is proving to be a problem, however.
|
by: Mike Curry |
last post by:
Just thought I'd post a few links to what I've done to share with others
(not that its anything great, but I think starting a thread like this would
be a good way for others to see what most of us have done- will be
interesting!). Check them out... Any comments, questions or interest, just
email me.
Distributed Computing Related (RSA/XBOX)
http://www.theage.com.au/articles/2003/06/06/1054700380639.html?oneclick=true
|
by: aling |
last post by:
Given: signed a, b;
How to judge overflow of the sum of these two operands? Just use one
*if* statement.
Is this statement right?
if ((a>0 && b>0 && sum<=0) || (a<0 && b<0 && sum>=0))
// overflow
else
// not overflow
|
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?
|
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 always 'a' stored as two's complement of '2'?
or
is always '-c' (two's complement of c) computed on the fly and the
resulting value is added to b ( b + (-c))?
| |
by: Mike Curry |
last post by:
Just thought I'd post a few links to what I've done to share with others
(not that its anything great, but I think starting a thread like this would
be a good way for others to see what most of us have done- will be
interesting!). Check them out... Any comments, questions or interest, just
email me.
Distributed Computing Related (RSA/XBOX)
http://www.theage.com.au/articles/2003/06/06/1054700380639.html?oneclick=true
|
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.
|
by: lovecreatesbea... |
last post by:
The following function determines the maximum of two integers. It
works on my machine.
If (a - a) is negative, what's the first bit of: (unsigned)(a
- a)? Is it 0 or 1?
#include <limits.h>
int max(int n1, int n2)
|
by: Adam W. |
last post by:
I'm dabbling with AVR's for a project I have and that means I have to
use C (ageist my will). Because my AVR will be tethered to my laptop,
I am writing most of my logic in python, in the hopes of using at
little C as possible.
In my quest I came across a need to pass a pair of sign extended two's
complement bytes. After painfully reading the wikipedia article on
what two's complement was, I then thought of how I would handle this
in...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |