Why do some hex numbers have a 2s compliment but some do not? - binary

I want to write out the 2s compliment of the hex 0x1234
As I understand the binary would look like: 0001 0010 0011 0100 and decimal 4660
I was under the impression changing the leading 0 indicates positive vs negative, however online calculators aren't giving me any answer so my understanding of this is wrong
Alternatively looking at 0x8001 as an example: the decimal would be 32769 and the binary 1000 0000 0000 0001
Online calculators return an answer of -32767 as the 2s compliment
Could anyone please offer an explanation or some other post that explains in detail this concept of 2s compliment? Thank you very much!

You get a number's two's complement by inverting all bits and then adding one to it. It must be agreed up front how many bits/digits are involved, i.e. what the word size is. For instance, the result will not be the same for 0x1234 and 0x00001234.
For your example (4 hex digits): 0x1234
In binary: 0b0001 0010 0011 0100
Invert bits: 0b1110 1101 1100 1011
Add one: 0b1110 1101 1100 1100
In hex: 0xEDCC

Related

Two's complement binary to decimal

I'm having trouble converting the following two’s complement binary number to decimal
01110000
Step 1: Invert the bits -> 10001111
Step 2: Add 1 to the bit -> 10010000
Therefore, the decimal value is 144
However, I used online converter and it says the decimal value is 112
The value of ...111 0111 0000 (padded with an infinite number of 1's) is -144. The value of ...000 0111 0000 (padded with an infinite number of 0's) is 112. Given the former, one could compute its additive inverse by inverting all the bits (yielding ...000 1000 1111) and adding 1 (yielding ...000 1001 0000, i.e. 144).

A 2's complement conversion

Honestly I'm doing some homework and got stuck with the 2's complement concept. So my question asked me to find the 2's complement for 492 and -1094(as 16-bit number) in both binary and hexadecimal. But I really got confused with the way it works. So I did 492's binary and mine was like 0000 0100 1001 0010 but I really got lost and don't feel right about my answer.. Can someone help me here... Thanks!
492 in binary goes to 0001 1110 1100
1*2^8 + 1*2^7 + 1*2^6 + 1*2^5 + 0*2^4 + 1*2^3 + 1*2^2 + 0*2^1 + 0*2^0
-1094 in binary goes to 0100 0100 0110
The trick to represent a negative number: you have to convert to binary and flip all the digits (0 becomes 1, and 1 becomes 0) and add 1.
So to calculate 2's complement, you just do the following:
For some number B, invert it's binary representation, and add 1.
For 492:
111101100
000010011 <-- invert
000010100 <-- add 1
Just do the same for your binary representation of -1094.

Converting hex into two's complement 8 bit binary

I am in a CSCI class and we are just learning about program execution. I am running a program called "Brookshear machine simulator" which was written by the author of the class text book ( Computer Science 11th edition by J. Glenn Brookshear). The program is intended to add the contents of 11 and 0F, storing the result into F1. I have done everything necessary and produced the hex value in 11 which is 09. I am then asked to convert this into two's complement 8-bit binary, which is where I am having a problem. I will be needing to convert some hex values into two's compl 8-bit binary in the future for this lab but I cant figure out how to do it. Can someone please help me understand what twos comp is and how is it related or the same as 8-bit binary , so I can convert this to two's complement 8-bit binary?
Here is a picture of the machine simulator with the inputs as directed by the lab instructions. My task is to find the hex value in 11 (09) then convert it to twos complement 8-bit binary.
Each hexadecimal digit has a 4 bit binary equivalent:
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
A 1010
B 1011
C 1100
D 1101
E 1110
F 1111
So if you have a two character hex value, like 09 then you can see that 0 = 0000 and 9 = 1001, so that would be:
00001001
which is an 8 bit value.
This works for any length of hex number of course, so for example 37FF in hex would be 0011011111111111 in binary.
Note that two's complement is irrelevant for your example as the number is positive.

Smallest Two's Complement in Binary

I'm not sure if I can ask a binary question here but here goes..
We had this question on our midterm but our professor hasn't provided a correct answer for it. It's been driving me crazy and the final is coming soon so it might be a good idea to fill this gap. Thanks!
Find the smallest two's complement number that, when added to 0101 0101 would result in an overflow. Express your answer in binary.
My reasoning:
I found the range of the original binary 0101 0101 by converting it to an actual number and then added one to it. Then I converted the number that was 1 more than the range into 8-bit binary as my answer. However, this only earned me 3/6 marks. I have no idea what else I could've done. Any insights would be greatly appreciated!
The original binary is a positive number (0 sign bit). Overflow occurs when you add a positive number to it that changes the sign bit. It should be easy to see what the smallest number is using binary notation:
No overflow:
0101 0101
+ 0010 1010
---------
0111 1111
Overflow:
0101 0101
+ 0010 1011
---------
1000 0000
I have no idea if this is what your prof was looking for. (You can probably just subtract from 1000 0000 instead of looking at it as a pattern.)
EDIT Since you asked for an example (meaning something different from the above), here's how subtraction would work:
1000 0000 (the target overflow quantity)
- 0101 0101 (the original binary)
---------
0010 1011 (the smallest number that will overflow when added to original)
That number is 85 in decimal, so 128-85 is 43

MIPS branches. How to jump backwards?

I've been looking for the answer quite a while and finally I decided to ask and see if someone here can help me ;)
The format for the conditional jump instruction in MIPS, lets say a beq instruction, contains a 16-bit field that indicates de "size" of the jump. I know it must be added to the current PC in order to obtain the desired direction. My doubt is, how do I set the field for a backwards jump? I guess two's complement? I found this example, but it confused me more than I was... if anyone could explain it briefly I'd be very thankful! By the way, sorry about my grammar/expressions, I'm not very good at english yet.
The example mentioned:
PC+Jump(Forward)
0100 1101 0000 0011 1010 1100 0101 1000
0000 0000 0000 0000 0000 0000 1001 1100
.......................................................................
0100 1101 0000 0011 1010 1100 1111 0100
PC+Jump(Backwards)
0100 1101 0000 0011 1010 1100 0101 1000
1111 1111 1111 1111 1100 0000 1001 1100
.......................................................................
0100 1101 0000 0011 0110 1100 1111 0100
The offset is a signed value, so jumping backward just uses a negative offset.
The examples you've shown demonstrate simple addition. In the context of branching and jumping, the top number represents the current PC, the second number is the offset, and the bottom number is the new PC. Recall that in two's complement, subtraction is the same as addition of a negative number. Notice that in the backward example, the offset is negative. When the offset is encoded in a MIPS instruction, it gets sign-extended to the full 32 bits.