Converting hex into two's complement 8 bit binary - 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.

Related

Can't understand how virtual address was converted to binary form

This is a question and answer from my operating system's textbook:
Question:
A certain computer provides its users with a virtual-memory space of 232 bytes.
The computer has 218 bytes of physical memory. The virtual memory is implemented by paging,
and the page size is 4096 bytes. A user process generates the virtual address 11123456.
Explain how the system establishes the corresponding physical location.
Distinguish between software and hardware operations.
Answer:
The virtual address in binary form is
0001 0001 0001 0010 0011 0100 0101 0110
When I plug "11123456" into a decimal to binary converter (like here: https://www.rapidtables.com/convert/number/decimal-to-binary.html), this is the result: 101010011011101100000000
which differs from the book answer.
Also, when I manually convert using a method like here: https://indepth.dev/the-simple-math-behind-decimal-binary-conversion-algorithms/
I still get something different.
I'm just confused how that virtual address was converted into binary form......
Thanks!
You think
11123456
is a decimal number but it isn't. The Computer representation of number is ever binary, octal or hexadecimal. In this case is Hexadecimal. If you convert the above number from hexadecimal to binary form you obtain the result:
0001 0001 0001 0010 0011 0100 0101 0110
Explanation:
Hexadecimal -> Binary
1 -> 0001
2 -> 0010
3 -> 0011
4 -> 0100
5 -> 0101
6 -> 0110
...

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).

Am I on the right track with my Hex to binary conversion?

I have to convert 1357AC.EF from hex to binary. I'm a little confused on what to do. Since it's a has a decimal, do I convert it from hex to decimal doing (1x16^5)+(3x16^4)+(5x16^3)+(7x16^2)+(10x16^1)+12+(14x16^-1)+(15x16^-2) and then convert that to binary by dividing that by 2 and finding the remainder? Or am I making this too hard for myself?
Just convert each individual hexadigit to its four-bit binary equivalent.
1357AC.EF would be 0001 0011 0101 0111 1010 1100 . 1110 1111
You are making it too hard one hex character is 4 bits e.g. 'A' = '1100'
So just iterate through the string get a character, look up the binary in a hash or an array, and then concatenate it with the earlier result.

Change 8-digit hexadecimal number(IEEE-754) single precision representation into a real number

I am changing an 8-digit hexadecimal number to a real number Y.
The number is 0xAC0396ED
My question is: Shall I take into consideration the 0x? What is the significance of the 0x?
I researched it and based on wikipedia, I got this:
"use the prefix 0x for numeric constants represented in hex"
What I plan on doing is take the part AC0396ED and change it to binary, then from binary manipulate the 32 bit number byte dividing the number intro 3 parts: sign, exponent, and fraction.
My last question is why do we need hexacdecimal, decimal, octal? Why don't we just stick to binary in all our arithmetic and operations?
Thank you.
My question is: Shall I take into consideration the 0x? What is the significance of the 0x?
No - you should not take that into consideration - its only a notation for hexadecimal base system
What I plan on doing is take the part AC0396ED and change it to binary, then from binary manipulate the 32 bit number byte dividing the number intro 3 parts: sign, exponent, and fraction.
Here is how you calculate it:
AC0396ED =>
10 12 0 3 9 6 14 13 in decimal. Then to binary
1010 1100 0000 0011 1001 0110 1110 1101
here you have all bits i.e 32 bits
1 | 01011000 | 00000111001011011101101
so the first bit is 1 => the number is negative
the second is the exponent
the third is the mantissa
My last question is why do we need hexacdecimal, decimal, octal? Why don't we just stick to binary in all our arithmetic and operations?
Its much more easy to stick to hexadecimal system - compare the 32 bits to the AC0396ED

how to do two complement multiplication and division of integers?

I have read this post on binary multiplication using two complement. but it is not very clear to me. Even I have difficulty understanding the wiki article on this. I want to know how to go about calculating multiplications of negative numbers:
eg: -1 with -7 should give 7.
A 4-bit, 2's complement of -1 is : 1111
A 4-bit, 2's complement of -7 is : 1001
some step-wise way of calculating the multiplication will be helpful. No article I came across talks about division. How to approach this?
step 1: sign extend both integers to twice as many bits. This is safe to do, though may not always be necessary.
for 4-bit --> 1111, you would extend as 1111 1111
for 4-bit --> 0111,you would extend as 0000 0111
step 2: do elementary multiplication
sep 3: take the correct number of result bits from the least significant portion of the result.
eg: after multiplication, you end up with something such as 0010011110 take the last 8 bits i.e 10011110
Let me illustrate with the example you provided: -1 X -7 in 4-bit representation
1111 1111 -1
x 1111 1001 x -7
---------------- ------
11111111 7
00000000
00000000
11111111
11111111
11111111
11111111
11111111
----------------
1 00000000111 ---> 7 (notice the Most significant bit is zer``o)
-------- (last 8-bits needed)
you could get more details here;
for division: convert to positive and after the calculation adjust the sign. I will leave this as exercise but you could refer this page.
Okay, let's see if I can make this simple enough for you.
Two's complement:
IFF (If and only if) you have a negative number, first put it into the positive form. For sake of simplicity, all numbers will be 6 bit. The limit of the bits will limit how big your numbers can go. Besides that, what the size is doesn't matter.
Some numbers converted to their positive binary form
-7: 000111
16: 010000
-22: 010110
1: 000001
Now for -7 and -23 ONLY we'll do two's complement on. So we flip the bits (1 -> 0 && 0 -> 1) and then add one.
000111
Goes to the complement + 1
111000
+ 1
=111001
And for 22
010110
Goes to the complement + 1
101001
+ 1
=101010
Then you just add them together like you would any other number.
And it looks like somebody else already covered the multiplication part, so I won't bother repeating that.