Is this an overflow arithmetic calculation? - overflow

So when I read the book and it says that overflow can't occur when add different signs and subtraction of the same sign.
But I have question when I do this: 185 - 122
I converted binary of 122 to 2s complement and did the addition, which is different signs:
185+(-122) and when I add them together, I got the sign bit overflow to 100111111. But if I cut off the MSB on the left, it is the correct answer. Is it an overflow?

No, it isn't overflow - the overflow resulting from the addition of 2 1's in the MSB must just be discarded. From Wikipedia
To get the two's complement of a binary number, the bits are inverted, or "flipped", by using the bitwise NOT operation; the value of 1 is then added to the resulting value, ignoring the overflow which occurs when taking the two's complement of 0.
So in your example
185 10111001
122 01111010 -
Taking the 2's complement of 122 (One's complement +1)
01111010 => 10000110
Adding:
10111001 185
10000110 +(-122)
--------
00111111 (63)
=63
The overflow is ignored.
There are however rules for detecting overflow after doing the 2's complement :
If the sum of two positive numbers results in a negative result
If the sum of two negative numbers results a positive result

Related

Represent negative numbers in other bases

How do I represent negative numbers in non-10 bases, eg say in base 20. I know how to do this in binary using two's complement, but what would the equivalent of two's complement be in other bases?
For example, in base 20, the denary number 100 is represented as 50. How would I make this 50 signed? Would I need to convert it to binary, two's complement it, the convert it back to base-20? It seems a little long-winded.
In that case, negative base-20 50 (which is 100 in base-10) would be 7g, and positive would be just 50. But is this the standard way to represent negative numbers in other bases?
The generalisation of two's complement is radix complement.
The radix complement of an 𝑛 digit number y in radix 𝑏 is, by definition, 𝑏𝑛−𝑦. The radix complement is most easily obtained by adding 1 to the diminished radix complement, which is (𝑏𝑛−1)−𝑦
We must however agree what 𝑛 is. For instance, in binary we may say we have 32 bits, so 𝑛 = 32 in that case.
So for the example of base-20 we should make some definitions:
The digits are "0123456789𝑎𝑏𝑐𝑑𝑒𝑓𝑔ℎ𝑖𝑗".
𝑛 is 10 (an arbitrary choice, but it has to be made)
We can also define what the "(diminished) complementary digit" is for each digit. It is the digit that when added to the first will always yield the greatest digit (𝑗20 in this case). For example 220 + ℎ20 = 𝑗20, so ℎ20 is the complement of 220, and vice versa.
For your example number 5020, we proceed as follows:
Replace every digit of the 𝑛-digit representation with its complement:
So the diminished complement of 000000005020 thus becomes 𝑗𝑗𝑗𝑗𝑗𝑗𝑗𝑗𝑒𝑗20
To get the negation of 5020 we just need to add 1 to that:
−5020 = 𝑗𝑗𝑗𝑗𝑗𝑗𝑗𝑗𝑓020
It is important to not reduce this representation to use fewer digits -- we have to stick with the 𝑛-digit representation, otherwise it is not clear when a number is positive or negative.

Signed Magnitude Representation

Hi I had a Quick Question,
What does 81 represent in sign and magnitude based on 8 bits?
a. 129 b. -128 c. -127 d. -1 e. none of the above
My Logic is that it would be none of the above as 81 is a positive integer the signed magnitude representation would just be a bunch of 0's in front of it as the value is not negative, would I be correct in this assumption?
Yes none of the above is the answer.
In signed magnitude the most significant bit is used to represent the sign. The rest of the bits are used to represent the magnitude of the number. Here since 81 is positive the sign bit is 0. The magnitude bits are 1010001
81 is 01010001 when represented in 8 bits signed magnitude.

Two ways to represent 0 with bits

Let's say we want to represent a signed number with 5 bits where the first bit is used for the sign (+ or -) of the number. Then the zero can be represented by two bit representations (10000 and 00000).
How is this problem solved?
Okay. There are always two bit in binary 1 or 0
And then there could be any number of bits for example 1bit to 64bit
If the question is 5-bit string then it should be XXXXX where X can be any bit(1 or 0)
First bit(sign bit) we can have either +0 and -0. (Thanks #machinery)
So if it is positive, we put 0 at first position and if it is negative, we put 1 at first position.
Four Bits
Now, we got our first bit, we are left with another 4-bits 0XXXX or 1XXXX as the question asked for 0,
the rest bit will be zero.
therefore the answer is 00000 or 10000
Look how to convert decimal to binary and binary to decimal.

What is the rationale behind (x % 64) == (x & 63)? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Bitwise and in place of modulus operator
Can someone explain the rationale that makes both expressions equivalents? I know it only works because 64 is a power of two, but how can I logically or mathematically go from division to bitwise and?
The operation x % 64 returns the remainder when x is divided by 64, which (assuming x>0) must be a number between 0 and 63. Let's look at this in binary:
63dec = 0011 1111b
64dec = 0100 0000b
You can see that the binary representation of any multiple of 64 must end with 6 zeroes. So the remainder when dividing any number by 64 is the original number, with all of the bits removed except for the 6 rightmost ones.
If you take the bitwise AND of a number with 63, the result is exactly those 6 bits.
Each time you do a bit-shift this is the same as dividing by two. This is because a binary representation is base 2. It is the same way that removing the 3 from 123 in base 10 gives you 12, and that is like dividing 123 by 10.
% is the mod operator, which means the remainder of a division. 64 is 2 to the sixth power, so dividing by 64 is like shifting out six bits. The remainder of the division is those six bits that you shifted out. You can find the value of the six bits by doing a bitwise-and with only the lower six bits set, which is 63.
first one gives the remainder.
second one is short-circuit ( bit wise AND).
in bit wise AND, 63(in binary is 111111) so whatever is on LHS (x) is anded, resulting in same except the MSB. Ans so is the case with % with 64( binary 100000), divides and MSB remains the same.

What is the 2's complement of -17?

What will be the binary value of -17 and how to find the 2's complement of -17?
Assuming an 8-bit word, start with the binary form of 17. = 00010001
Then invert the bits: = 11101110
Then just add 1: = 11101111.
If you've got a 16-, 32- or 64-bit word then you'll have a load more leading 1s.
Even if you do not assume anything, you have to just keep the leftmost bit significant.
Start with the word itself, 10001.
Then invert gives the one's, 01110
Now add 1 to this number. 01111.
But to keep the left most bit significant, append a one there eg,101111
in terms of the minimum number of bits required (6 here).

Resources