Negative decimal fraction to binary

36 views (last 30 days)
Abdul Gaffar
Abdul Gaffar on 26 Apr 2018
Commented: Walter Roberson on 31 Aug 2021
Good afternoon sir, how to convert negative decimal fraction into binary (8 bits) and vice-versa? For eg. Convert - 0.0728 to binary (8 bits)
  12 Comments
Walter Roberson
Walter Roberson on 31 Aug 2021
Sorry, you are mistaken in this matter. Guillaume does understand matter, and he was entirely correct to repeatedly ask for details of which encoding method needed to be used.
It turned out in https://www.mathworks.com/matlabcentral/answers/397483-negative-decimal-fraction-to-binary#comment_565925 that the original poster wanted 1 bit (MSB) for sign, 7 bits for exponent and 8 bits for the fraction --- for a total of 16 bits, not 8. But then it turned out https://www.mathworks.com/matlabcentral/answers/397483-negative-decimal-fraction-to-binary#comment_565932 that the original poster wanted IEEE 754 Half-Precision, which is 1 bit (MSB) for sign, 5 bits for exponent, and 10 bits for the fraction -- for a total of 16 bits, not 8.
I would ask you, Alex, how, without pressing the original poster repeatedly to define which floating point format was to be used, that we could have known that the original request for 8 bit representation was really a request for s5e10 representation? Guillame's very first response mentioned IEEE 754 Half-Precision, but the original poster continued to ask for 8 bit representation https://www.mathworks.com/matlabcentral/answers/397483-negative-decimal-fraction-to-binary#comment_561153 -- with the original poster having been pointed to IEEE 754 Half-Precision and the original poster having said "No" to that, that they needed 8 bit representations, would it not have been very disrespectful for us to have insisted to the original poster that they were wrong about their needs ???

Sign in to comment.

Answers (2)

John D'Errico
John D'Errico on 26 Apr 2018
Edited: John D'Errico on 26 Apr 2018
There is no tool to do so. (That I know of, although it has been on my to do list for a while.)
A decimal fraction like that will not have an exact representation in only 8 bits. So any solution will only be a rounded binary one.
Next, you show a negative number there. Do you really intend to store the sign as one of those 8 bits? In that case, you only have 7 bits of precision.
What binary representation will you employ that includes negative numbers? Which of those 8 bits is the sign bit?
A simple scheme might be, assuming the number is less than 1, AND we ignore the question of sign...
% You can do anything you want with the sign bit.
D = abs(-0.0728);
nbits = 8;
B = zeros(1,nbits);
for i = 1:nbits
D = D*2;
if i < nbits
B(i) = floor(D);
else
B(i) = round(D);
end
D = rem(D,1);
end
So, if I set nbits = 32, we find B as:
B
B =
Columns 1 through 30
0 0 0 1 0 0 1 0 1 0 1 0 0 0 1 1 0 0 0 0 0 1 0 1 0 1 0 1 0 0
Columns 31 through 32
1 1
Is it a good binary representation?
sum(B.*(1/2).^(1:nbits))
ans =
0.0727999999653548
So that is indeed a binary expansion of abs(D), in terms of negative powers of 2.
I could have done it more simply as:
D = abs(-0.0728);
nbits = 32;
B = dec2bin(round(D*2^nbits),nbits)
B =
'00010010101000110000010101010011'
The number is the same, but here, represented in character form, which is arguably more efficient. To verify:
sum((B-'0').*(1/2).^(1:nbits))
ans =
0.0727999999653548
But you want only 8 bits. Be careful, as 8 bits of precision is pretty crappy.
B = dec2bin(round(D*2^nbits),nbits)
B =
'00010011'
What number does that actually represent?
sum((B-'0').*(1/2).^(1:nbits))
ans =
0.07421875
As I said, 8 bits is crap for precision, but that is your choice to make. If one of those bits had to be a sign bit, worse yet.

Walter Roberson
Walter Roberson on 27 Apr 2018
bitstream = reshape((dec2bin(typecast(LH, 'uint8'),8) - '0').', 1, []);
  6 Comments

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!