Greetings...
I have bunch of 8 binary data which I did like to convert into decimal format.
8-bit is divided as follows, like typical 1QN format:
[7] bit = Sign
[6] bit = Integer
[5:0] bit = Fraction
For example: 11010011 decimal equivalent will be -0.707.
Please help me to get this converted into decimal representation.
Thanks
> I have bunch of 8 binary data which I did like to convert into decimal
> format.
>
> 8-bit is divided as follows, like typical 1QN format:
>
> [7] bit = Sign
> [6] bit = Integer
> [5:0] bit = Fraction
>
> For example: 11010011 decimal equivalent will be -0.707.
I don't see how 11010011 can be equivalent to -0.707 .
If you had said -0.703 I could come up with an explanation, but it would
be an unusual format:
010011 is binary 19 in a field of 6 bits, which would be 19/64 .
The negative sign is set, and so is the integer bit. If we take -1 and
*add* 19/64 then the result would be 0.703125
Numbers of this form would be able to represent -1 to +2, but there
would be an ambiguity in representation: sign set and integer not set
would have to denote starting from 0 and adding the fraction, but that
would be the same as if the sign and integer were not set.
It seems most probable to me that your example of -0.707 is incorrect.
-1.296875 would make more sense as the answer to what 11010011 represents.
> 010011 is binary 19 in a field of 6 bits, which would be 19/64 .
> The negative sign is set, and so is the integer bit. If we take -1 and
> *add* 19/64 then the result would be 0.703125
Correction, the result would be -0.703125
Hi Walter,
Thanks for your reply.
I should have explained by situation fully. So let me reiterate the whole thing from the source itself:
Range is -1 <= Xin <=1. Input vector Xin is expressed as a pair of fixed-point 2’s complement numbers with an integer width of 2-bits (1QN format).
In this example, the input width is set to 10 bits
Xin : “0010110101” => 00.10110101 => 0.707
Xin : “1101001011” => 11.01001011 => -0.707
And sure enough this is what I get when I run following command in MATLAB
a = fi(0.707,true,10,8)
bin(a)
a = 0.7070
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 10
FractionLength: 8
ans = 0010110101
and
a = fi(-.707,true,10,8)
bin(a)
a = -0.7070
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 10
FractionLength: 8
ans = 1101001011
Thanks again
Guys, any help?
> Guys, any help?
Your information about the format was insufficient. If your available
range is +/-1 (exclusive) then the "integer" bit is not needed. The
existence of that bit leads to undefined behaviour, such as what
011000000 should represent.
If not for the "integer" bit, then you would take the last 8 bits as
uint8, cast to double, and divide by 256. Then, if the sign bit is set,
subtract 1; if the sign bit is clear, leave the number alone.
>> bin2dec('01001011') ./ 256 - 1
ans =
-0.70703125
>> bin2dec('10110101') ./ 256
ans =
0.70703125
But with the integer bit there, we don't know what to do.
Hi Walter,
Even though from mathematical stand point you're right, but your above solution did the tick. This was what I was hoping to get.
Thanks a lot