Trouble with 32-bit two's complement hexadecimal conversion - hex

I am working on a set of problems where I need to convert 32-bit two's complement hexadecimal values into regular decimal values. Now, my book tells me that whenever I see a hexadecimal value leading with 8 through f, it will always be negative.
The problem in question is: 0xfffffe58
In my understanding, this means that I only start evaluating after the chain of 'f' values, leaving me with evaluating 'e58'. 'e'will be converted into -2, '5' will stay the same, and '8' will become -8. Multiplying all the values out, i get
(-2*16^2)+(5*16)+-8 = -512+80-8 = -440.
Now, the answer is -424, so I am confused as to why the 8 would be positive, since the leading binary digit for 8 is 1, and we are using two's complement, it would lead me to believe that it would be -8.

In two's compliment it is only the most significant bit that signals that is is a negative number. So it's only the first hexadecimal digit is 8-f that you need to check. The less significant digits do not impact the determination. So the 8 is not negative, it is just part of the number.
The way you are trying to do your conversion isn't correct either - you don't do a digit by digit conversion like you are trying to do, that's not how two's compliment works.
One way to do the conversion is to to a binary flip and then add one. So F -> 0, E -> 1, etc.
So you would have F F F F F E 5 8 -> 0 0 0 0 0 1 A 7 + 1 = 0 0 0 0 0 1 A 8 = 424

You're completely on the wrong track with your calculation. e doesn't convert to -2; 5 doesn't stay the same.
You need to find the matching digit so that each pair adds up to F (except the last which adds to 10):
F F F F F E 5 8
0 0 0 0 0 1 A 8
The equation is that 0xFFFFFE58 + 0x000001A8 = 0x100000000 . 1A8 is 424.

Related

integer divison with negative remainder

I'm trying to find a name for mathematical division operation which can produce negative reminder.
Examples of expected:
5 %? 2 = 2*2 + 1
11 %? 3 = 4*3 - 1
module of reminder should be as small as possible.
Does someone knew the name of such operation?
Computer language remainder functions return a positive remainder. What you need is simple. In your second example you want is a remainder of -1.
11 %? 3 = 4*3 - 1
But what you get is a remainder of 2.
11 %? 3 = 3*3 + 2
As I said in the comments, check the remainder 2 against half the divisor 3/2 = 1.5. Since the remainder is greater than half the divisor, subtract the divisor:
2 - 3 = -1
Yes, you will have to store a real number for half the divisor.

Convert binary, octal, hexa FLOATS to decimal "manually" (ex: [135.263]B10 -> [?]B2)

I'm a student in programmation and I have a course called "Informatic Mathematics". In the exercices it's asked to convert floating numbers from decimal, octal, hexadecimal or binary to another base (not necesserly to base 10) and keep 12 digits after the comma(or the dot) if it is possible. For exemple:
(135.263)b10 => b2
(100101001.11)b2 => b10
(1011110010.0101)b2 => b8
...
I know how to convert numbers. The way I convert the decimal part (after the dot) is to divide this part by the highest multiple of the target base until I get 0 or until I reach the 12th digits after the dot. The problem is that I don't know all the negate multiples of 2 so usually I write them on a separate sheet, but usually I don't have to keep 12 digits after the dot and writing these multiples on a seperate sheet takes time and during the exam, time is a precious thing and I can't waste it to write these multiples.
So I would like to know if there's a better way to do these conversions or if anyone has any tips.
Also, when I convert from non-decimal number to another non-decimal number (ex: b2 => b8) I usually convert the first number to base 10 and then convert the base 10 number to the target base. I would like to know if there's a way to convert the first number directly into the target base without having to convert it in base 10 first.
BTW: Sorry if my english is a bit weird. I'm a french canadian and I did my best, but please let me know if there is something you do not understand well.
I'll start with b2 > b8.
001 011 110 010.010 100
As you see, I've separated the number into 3 digit segments (2^3 = 8). You have to add extra 0 to the left and to the right to make it like that. Then you convert it digit by digit. In this case you'll receive 1352.24
b2 => b10
Some harder math here. Mark digits in your number this way:
1 0 0 1 0 1 0 0 1 . 1 1
8 7 6 5 4 3 2 1 0 -1 -2
Then calculate fractional and whole part
2^0 + 2^3 + 2^5 + 2^8 + 2^-1 + 2^-2
b10 => b2
Multiple the fraction by 2 till you get 1. From each multiplication you take the whole part. Example:
0.25 * 2 = 0.5; 0.5 * 2 = 1;
Thus, 0.25 is 0.01;
UPD For negative conversions check out first and second complement.

Solve logical and mathematical Equation

I would like to solve following equation:
x^2-bitxor(2,x)=0
Is it possible ?
bitxor(2,x) wil either add 2 or subtract 2 to/from x, so you have to solve both:
x^2 - x + 2 = 0
and:
x^2 - x - 2 = 0
and then test to see if the any of the solutions work in the original expression.
In the first case the roots are complex, in the second case the roots are -1 and +2.
Substituting back into the original equation:
x = -1 => (-1^2) - bitxor(2, -1) = 1 - (-3) = 4
x = 2 => (2^2) - bitxor(2, 2) = 4 - 0 = 4
So apparently there is no real integer solution.
There are solutions in modular arithmetic, which is precisely the arithmetic that most computers use for unsigned integers. For example, consider the case of modulo 256 arithmetic. In C and C++, this is the arithmetic of unsigned char. Here we have two solutions, 91 and 166. (See http://www.wolframalpha.com/input/?i=x%5E2+mod+256+%3D+x+-+2%2C+x+%3E+0%2C+x%3C+256 ).
In fact, for any modulo 2^N system, there will be two solutions that sum to 1 (modulo 2^N).

What does a circled plus mean?

I cannot understand the calculation "66 ⊕ fa = 9c".
The sum is clearly over "ff", so I am confused.
The topic is simple encryption algorithm.
What does a circled plus mean?
People are saying that the symbol doesn't mean addition. This is true, but doesn't explain why a plus-like symbol is used for something that isn't addition.
The answer is that for modulo addition of 1-bit values, 0+0 == 1+1 == 0, and 0+1 == 1+0 == 1. Those are the same values as XOR.
So, plus in a circle in this context means "bitwise addition modulo-2". Which is, as everyone says, XOR for integers. It's common in mathematics to use plus in a circle for an operation which is a sort of addition, but isn't regular integer addition.
This is not an plus, but the sign for the binary operator XOR
a b a XOR b
0 0 0
0 1 1
1 0 1
1 1 0
It's not an addition, but an exclusive OR operation. At least the output confirms to the same.
That's the XOR operator, not the PLUS operator
XOR works bit by bit, without carrying over like PLUS does
1 XOR 1 = 0
1 XOR 0 = 1
0 XOR 0 = 0
0 XOR 1 = 1
Hope this layout works, take it to the binary representation with an XOR:
66h = 102 decimal = 01100110 binary
FAh = 250 decimal = 11111010 binary
------------------------------------
10011100 binary <------ that's 9Ch/156 decimal
XOR rules are basically:
1 XOR 1 = 0 false
1 XOR 0 = 1 true
0 XOR 0 = 0 false
but the wiki I linked earlier will give you more details if needed...thats what it looks like they are doing in the screenshot you provided
The plus-symbol in a circle does not denote addition. It is a XOR operation.
It's an exclusive or (XOR). If I remember correctly, when doing bitwise mathematics the dot (.) means AND and the plus (+) means OR. Putting a circle around the plus to mean XOR is consistent with the style used for OR.
I used the logic in the replies by rampion and schnaader. I will summarise how I confirmed the results. I changed the numbers to binary and then used the XOR-operation. Alternatively, you can use the Hexadecimal tables: Click here!
It is XOR. Another name for the XOR function is addition without carry. I suppose that's how the symbol might make sense.

Adding negative and positive binary?

X = 01001001 and Y = 10101010
If I want to add them together how do I do that? They are "Two's Complement"...
I have tried a lots of things but I am not quite sure I am getting the right answer since there seems to be different type of rules.
Just want to make sure it is correct:
1. Add them as they are do not convert the negative
2. Convert the negative number you get and that's the sum.
f.eks
01001001+10101010 = 11110011 => 00001100 => 1101 => -13
Or?
1. Convert the negative
2. Add them together and convert the negative
f.eks
01001001+10101010 => 01001001 + 01010110 => 10011111 => 01100001 => -97
So basically what I want to do is to take: X-Y, and X+Y
Can someone tell me how to do that?
Some resource sites:
student-binary
celtickane
swarthmore
The beauty of two's complement is that at the binary level it's a matter of interpretation rather than algorithm - the hardware for adding two signed numbers is the same as for unsigned numbers (ignoring flag bits).
Your first example - "just add them" - is exactly the right answer. Your example numbers
01001001 = 73
10101010 = -86
So, the correct answer is indeed -13.
Subtracting is just the same, in that no special processing is required for two's complement numbers: you "just subtract them".
Note that where things get interesting is the handling of overflow/underflow bits. You can't represent the result of 73 - (-86) as an 8-bit two's complement number...
Adding in two's complement doesn't require any special processing when the signs of the two arguments are opposite. You just add them as you normally would in binary, and the sign of the result is the sign you keep.
And just to make sure you understand two's complement, to convert from a positive to a negative number (or vice versa): invert each bit, then add 1 to the result.
For example, your positive number X = 01001001 becomes 10110101+1=10110110 as a negative number; your negative number Y = 10101010 becomes 01010101+1=01010110 as a positive number.
To subtract Y from X, negate Y and add. I.E. 01001001 + 01010110.
Your confusion might be because of the widths of the numbers involved. To get a better feel for this you could try creating a signed integer out of your unsigned integer.
If the MSB of your unsigned integer is already 0, then you can read it as signed and get the same result.
If the MSB is 1 then you can append a 0 to the left to get a signed number. You should sign-extend (that is, add 0s if the MSB is 0, add 1s if the MSB is 1) all the signed numbers to get a number of the same width so you can do the arithmetic "normally".
For instance, using your numbers:
X = 01001001: Unsigned, MSB is 0, do nothing.
Y = 10101010: Signed, did nothing with X, still do nothing.
But if we change the MSB of X to 1:
X = 11001001: Unsigned, MSB is 1, Add a 0 --> 011001001
Y = 10101010: Signed, extended X, so sign-extend Y --> 110101010
Now you have two signed numbers that you can add or subtract the way you already know.
01001001 + 10101010 = 11110011 => 00001100 => 1101 => -13
The first addend is 73. The second addend is -86. 86 = 101010. Padding to 8 bits including the 1 for the negative sign, -86 = 10101010
Both addends are in Sign-bit representation.
Solving them their sum is 1 1 1 1 0 0 1 1 which is an encoded binary (equivalent to having undergone One's Complement by inversion then Two's Complement by adding 1).
So do the reverse to have the decimal number. This time do first subtract 1 as inverse of Two's Complement = 1 1 1 1 0 0 1 1 - 1
= 1 1 1 1 0 0 1 0 then invert as in One's Complement = 0 0 0 0 1 1 0 1 which is equal to 13. Having done such reversal or having acknowledged One's complement and Two's Complement, the answer is negative. So affix the negative sign = -13

Resources