473,748 Members | 11,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Two's complement integer divided by Powers of 2 question

I got this question from my textbook and I cannot understand the theory.
When a signed positive integer X divided by pow(2,k), the result is shifting
k bits to right and putting w-k bits of 0 from the most significant bits.
However, when the X is a negative number divided by pow(2,k), the shifting
and sign bit extension method doesnt give the correct answer?
What kind of bias should we make on the X before the division?
Thanx
Nov 15 '05 #1
3 6122
Janice wrote:
I got this question from my textbook and I cannot understand the theory.
When a signed positive integer X divided by pow(2,k), the result is shifting
k bits to right and putting w-k bits of 0 from the most significant bits.
However, when the X is a negative number divided by pow(2,k), the shifting
and sign bit extension method doesnt give the correct answer?
What kind of bias should we make on the X before the division?
Thanx


The "theory" is simple enough. Let's work through
the example of dividing -6 by 2. In two's complement,
-6 looks like 11...1010. Shifting this right one bit
position and inserting a new 1 in the vacated leftmost
spot gives 11...1101, which is -3, half of -6. Everything
looks good so far.

But now if we try the same thing starting with -3,
things don't work quite so well. Doing another shift
produces 11...1110, which is -2. However, the result
of the integer division -3/2 is not -2, but -1: Integer
division discards the fractional part of the "true" quotient,
so we get -3/2 => -1.5 => -1. (Historical note: the first
C Standard didn't define integer division quite so rigidly,
and allowed -3/2 to be either -2 or -1. The latest Standard
has tightened the rules to follow the practice adopted by
other programming languages.)

You can probably see what's happening: integer division
"truncates toward zero" while right-shifting "truncates toward
minus infinity." For even X there's nothing to truncate and
you'll get the same answer either way, and for positive X
zero is in the same direction as minus infinity. But for odd
negative X, zero and minus infinity are in opposite directions
and the two operations give different answers.

There's a further complication: C doesn't define what
happens when a negative number is right-shifted. This is a
nod toward different instruction sets on different machines:
some will fill the vacated sign position with a copy of the
original sign bit, while others will fill with a zero bit
(these are sometimes called "arithmetic shift" and "logical
shift" operations). Right-shifting -6 and supplying a new
zero bit would transform 11...1010 to 01...0101, a large
positive number which is clearly not a good approximation
to -3! The C Standard doesn't even require that either of
these results be obtained; it simply says "all bets are off"
if you right-shift a negative number.

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 15 '05 #2
Janice wrote:
I got this question from my textbook and I cannot understand the theory.
When a signed positive integer X divided by pow(2,k), the result is shifting
k bits to right and putting w-k bits of 0 from the most significant bits.
However, when the X is a negative number divided by pow(2,k), the shifting
and sign bit extension method doesnt give the correct answer?
What kind of bias should we make on the X before the division?
Thanx


Note that pow(2,k) is a double. Division of integer by double is done in
floating point and yields double. What are you trying to do? What do you
think the "correct answer" to any of this is?

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 15 '05 #3
Janice wrote:
I got this question from my textbook and I cannot understand the theory.
When a signed positive integer X divided by pow(2,k), the result is shifting
k bits to right and putting w-k bits of 0 from the most significant bits.
However, when the X is a negative number divided by pow(2,k), the shifting
and sign bit extension method doesnt give the correct answer?
What kind of bias should we make on the X before the division?


This is a bit involved.

1) Floating point rounds towards zero, whereas right shifting usually
rounds towards INT_MIN. (Using pow(2,k) forces promotion to floating
point.)

2) The ANSI C standard does not dictate anything useful about how right
shift works. Basically some old obsolete hardware that the ANSI C
standard committee insists on continuing to coddle cannot perform
correct right shifting. Rather than forcing that very marginal old
hardware to emulate the correct behavior (much as they forced the most
popular microprocessor family of all time, and widely deployed
desktop/workstation processor in history to emulate FP before they
could manage to include on-chip support for it) for this less commonly
used operation, the standard just decided not to define a single
behavior for it.

So whether or not some calculation is equal or consistant with right
shift is not up to the numerical or mathematical properties of ordinary
calculations. Its actually up to the capriciousness of the compiler
implementor. (Though its not surprising that your textbook thinks this
behavior deterministic, since 99.9999999% of all platforms do perform
correctly.)

--
Paul Hsieh
http://www.pobox.com/~qed/

Nov 15 '05 #4

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

Similar topics

8
14376
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?
18
2641
by: Toby Newman | last post by:
I need to randomly choose one of four paths in my program. Using the tools I know, the best way I can think to do it is by doing something like the following: //============================== #include <stdlib.h> //allow rand() function int i; i = rand (); // i = any number of the set or if (0<=i<(4294967295/4))
33
7708
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
2125
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 number and its complement are added the result must be a
14
10518
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.
6
3396
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 topic to this group. Try to ignore CBF's errors in the 1's complement -1 and sign magnitude -1...
29
3889
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)
35
3653
by: aarklon | last post by:
Hi all, The following question is asked frequently in interviews How to find the greatest of 2 numbers without using relational operators ? the solution i have seen is ( a+b + abs(a-b) ) /2 ;
0
8989
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8828
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9367
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9319
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,...
0
8241
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6795
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4599
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2213
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.