Converting a decimal number into a Q3.4 fixed point representation - binary

How can I convert - 4,5_10 into the Q3.4 fixed point representation?
My suggestion would be:
4_10 = 100_2
0,5 * 2 = 1,0
0 * 2 = 0,0
Result: 0100 0010_2
Inverting the result to: 1011 1101_2
And adding +1 to the one's complement
= 1011 1110_2C (Two's complement)
Would that be right? I tried it on another way, but it I still get a different result.

Related

Two's Complement Addition Resulting In A Negative

I understand the concept of using two's complement to represent a negative value which can then be used in addition to find the resulting value.
ex.
x = 15 and y = 10, in binary x = 1111 and y = 1010
for calculating x - y, we represent y as a negative using twos complement which results y = 0101 + 0001 = 0110 then carrying out the addition x + (-y) = 1111 + 0110 = 0101 which translates to the expected result 5.
All is well and fine until I try to use the same method to calculate y - x which ends up resulting in 1011 translating to 11, not the expected result -5. Is there a way to properly calculate the addition of two binary numbers resulting in a negative value?
Think of it this way; y - x is equivalent to -x + y. For that reason, we can first negate x, which is 15 (1111 in binary), which becomes 0001 after negation. We can then add that value to y (which is 10, 1010 in binary), which results in 1011. Because this value is negative, we then flip the bits and add 1 to make it positive, and we get 0101, which is 5 in base-10. This means that our result was -5, which is correct.
I think you were correct everywhere except where you stated that it translates to 11.

How Many Values Can Be Represented With n Digits in Hexadecimal Systems?

I have n = 7. How many different values can be represented using 7 digits in In Hexadecimal Systems?
I approached it this way:
I set each bit of the 7 to be equal to 1. Therefore the highest number I get is 111 1111 = 16^0 + 16^1 + 16^2 + 16^3 + 16^4 + 16^5 + 16^6 which is equal to 17895697. I also consider zero being part of the answer, so my range is 0 to 17895697 and therefore, I get 17895698 different values. however, I know that in binary you would do 2^7 = 128. does it apply to hexadecimals as well? If I do it this way I get 16^7 = 268435456 which does not equal to what I got before.
Is any of my answers correct? if not, could someone please explain what it the right way to do this question?
It may be more simple explanation that you would expect, but it does not mean it would not work.
One hexadecimal digit can represent one of 16 values (0x0 to 0xF, or 0 to 15 if you prefer), so 16^7 = 268,435,456 and that's how many different values you can achieve if you use all the bits.
0000 0000 0000 0000 0000 0000 0000 to 1111 1111 1111 1111 1111 1111 1111.
The general formula is the sum of digit * base^place
Consider decimal numbers. What's the largest number you can represent with 3 decimal digits? The largest possible digit is 9. Therefore the max number is:
9*10^2 + 9*10^1 + 9*10^0 = 9*100 + 9*10 + 9*1 = 999
The next largest number would with be 1*10^3 = 1000
Another way to calculate the max of decimal three digits, is to get the smallest number with 4 digits and subtract 1.
The smallest decimal with 4 digits is 1000 or
1*10^3 + 0*10^2 + 0*10^1 + 0*10^0 = 1000
Now subtract 1
1000 - 1 = 999
The possible digits for a hex num are 0..F (values 0..15)
For a 7 digits the places range from 6 down to 0.
The largest possible number is therefore:
15*16^6 + 15*16^5 .... 15*16^0
Or the smallest number in hex with 8 digits is #10000000
1*16^7 + 0*16^6 ... + 0*16^0 = 1*16^7
So the largest possible number with in hex with 7 digits is
1*16^7 -1
16^7 is the correct answer. Why? Because for every character you add there are 16 times the previous possibilities. So for 7 characters it's 16^7.
Consider binary - 2^1, you get 0 and 1, 2 possible values. 2^2, you get 0 through 3 (00,01,10,11), 4 possible values.
Using hex, it's 16^7 possible values, which is 268,435,456 possible values.

Two's complement binary multiplication

I'm confused about where I'm going wrong in the following problem using binary multiplication with two's complement.
I am trying to multiply 12 * -6.
We know that 12 = 01100 and -6 = 11010, and sign-extended we get 00000 01100 * 11111 11010. I tried multiplying these two numbers as follows:
1111111010
x 0000001100
------------
0000000000
0000000000
1111111010
+ 1111111010
---------------
10111110111000
This is definitely not -72, so what am I doing wrong?
Drop the digits from the left that don't fit in the data type:
10111110111000
truncates to
1110111000
You'll find that this is indeed -72.

Understanding Two's Complement Addition

I've built a four bit adder/subtractor utilizing 4, 1 bit full adders and the input and output are twos complement numbers.
If X=0111 and Y=1000 their sum is obviously 1111.
In decimal this is equivalent to 7 + 8 thus 15 which is what the sum results in.
I am confused however if this result needs to be translated back into "regular" binary by flipping the bits and adding one? So that the answer would be 0001 representing 1 in decimal instead. And that Y in decimal before translation was actually 0110 representing 6 thereby yielding the following in binary 7-6 = 1. If anyone could point me in the right direction I would appreciate it!
It appears you've got the conversion for Y wrong. Y = 10002 = -810.
To represent -6 you take 0110, flip the bits to get 1001 and add one, so Y = 1010. (And 0111 + 1010 = 0001 as you expect.)
To go back, flip the bits of 1010 = 0101 and add one giving 0110 = 6.
Edit to answer your follow-up question:
Let:
X = 0111
Y = 1100
X + Y = 0011 (ignoring overflow)
So whatever we're adding, it equals 3. We know that X = 7.
Y = 1100 => 0011 + 1 = (negative)0100 = -4
7 + (-4) = 3
No translation is necessary, just represent the positive and negative numbers correctly. I think your confusion is coming from the fact that we're "negating" the negative numbers to find the absolute value of that number and sticking a negative sign in front of it, as in the conversion of Y above. That's because negative numbers in 2's complement aren't as readable as positive numbers, but 0100 is still +4 and 1100 is still -4.

What is the "biggest" negative number on a 4-bit machine?

Or, what is the range of numbers that can be represented on a 4-bit machine using 2s-complement?
That would be -8 to +7
The range is -8 to 7, or 1000 to 0111. You can see the full range here.
4 bits (using 2's complement) will give you a range from -8 to 7.
This should be straightforward to work out yourself.
Range in twos complement will be:
-1 * 2 ^ (bits - 1)
to
2 ^ (bits - 1) - 1
So for 4 bits:
-1 * 2 ^ (4 - 1) = -1 * 2 ^ 3 = -8
to
2 ^ (4 - 1) - 1 = 2 ^ 3 - 1 = 7
Also, if you are interested and for others maybe browsing this question -
twos complement is used for easy binary arithmetic:
to add - you just add the two numbers without conversion and disregard the overflow:
-6 + 7 = 1
is
1010 = -6
0111 = 7
------
(1)0001 = 1 (ignoring the overflow)
...and more yet - to convert a negative binary number to its opposite positive number:
if the sign bit (highest order bit) is 1, indicating negative... reading from least significant to most significant bit (right to left), preserve every bit up through the first "1", then invert every bit after that.
So, with 8 bit
10011000 .. becomes
01101000 (* -1) = 104 * -1 = -104
and that is why 10000000 is your lowest negative number (or in X bit 1000.all zeroes..000), it translates to unsigned 10000000 * -1 = -128
Maybe a long winded answer but to those without the 1s and 0s background I figure it is useful
Well let's dissect this question.
Firstly, the question should be framed as - "The least*(because it is negative, so biggest is not the right usage)* possible negative number to be stored in 4-bit would be?"
Numbers are of two types -
Signed (holds both + and - numbers )
Unsigned (holds only + numbers)
We will be using binary representation to understand this.
For Unsigned -
4-bit minimum number = 0000 (0)
4-bit maximum number = 1111 (255)
So range would be Range : 0-15
For Signed 4-bits, first bit represent sign(+/-) and rest 3-bits represent number.
4-bit minimum will be a negative number.
So replace the first bit(MSB) with 1 in 0000(least unsigned number), making it 1000.
Calculate decimal equivalent of 1000 = 1*2^3 = 8
So, number would be -8 (- as we have 1 as the first bit in 1000)
4-bit maximum will be a positive number.
So replace the first bit(MSB) with 0 in 1111(largest unsigned number), making it 0111.
Calculate decimal equivalent of 0111 = 1*2^2 + 1*2^1 + 1*2^0 = 7
So, number would be +7 (+ as we have 0 as the first bit in 0111)
Range would be -8 to +7.