Negative decimal fraction to binary

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 Commenti

What encoding would you be using? The minimum number of bits for IEEE754 encoding is 16 (half-precision floating point). You could use a fixed-point encoding but you need to tell use how many bits are dedicated to the fraction.
Sir, actually i am working on stegonography using DWT. In high frequency band, we are using LSB method to embed the secret message. I want to convert pixel values,like (- 0.0728) into 8 bits and vice versa.
"actually i am working on stegonography using DWT. In high frequency band, we are using LSB method to embed the secret message"
Totally irrelevant
" I want to ..."
My feeling is you haven't even thought about what it means to encode a non-integer number with only 8-bits. In any case, until you specify what the 8-bit encoding is, we can't answer your question. There is no standard 8-bit encoding for non-integer numbers
John D'Errico
John D'Errico il 26 Apr 2018
Modificato: John D'Errico il 26 Apr 2018
Agreed. Especially if that includes a sign bit. At best, you can do something like multiply by 128, then round. That has you working with 7 binary digits, plus that blasted sign bit. So you are effectively working in int8 precision. That is, int8 has 7 binary bits, plus a sign bit.
Now, you can twiddle with the unit bit of the result, and finally divide by 128 to return to a decimal number.
Abdul Gaffar
Abdul Gaffar il 26 Apr 2018
Modificato: Abdul Gaffar il 28 Apr 2018
Dear Guillaume sir, Good afternoon, I have code below-
clc; clear all;
close all;
i=imread('lena.png');
figure,imshow(i) ;
[LL,LH,HL,HH]=dwt2(i,'db1');
how to convert LH band into 16 bit binary and vice versa.
Sir, can you provide the code for the same?
Sir, not satisfied with your answer.
There is no statisfaction guarantee on this forum.
You've been told repeatedly that there is no established ways of converting a fraction to 8-bit. And that whichever method you use will lead to significant loss of precision. We're still waiting for you to explain how the number is to be encoded as 8-bit.
Again, There is no standard 8-bit encoding for non-integer numbers
Good afternoon sir, thanks for previous replies. Sir, can you tell me, how to convert negative decimal fractions into binary (16 bits) and vice-versa ? For eg. - 170.5844
"how to convert negative decimal fractions into binary (16 bits)
Using what encoding? IEE754 half-precision? Something else?
My feeling is that you don't really understand what you are doing. Usually when you perform DWT you deal with integers, not fractions. Same with LSB encoding. This is usually done on integer values, not fractions.
Guillaume you have given an extremelly useless answer, might as well not have answered at all. You are the one that does not understand a very common application.
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 ???

Accedi per commentare.

Risposte (2)

John D'Errico
John D'Errico il 26 Apr 2018
Modificato: John D'Errico il 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.
bitstream = reshape((dec2bin(typecast(LH, 'uint8'),8) - '0').', 1, []);

6 Commenti

Dear Walter Roberson sir, Good evening , I have code below-
clc; clear all;
close all;
i=imread('lena.png');
figure,imshow(i) ;
[LL,LH,HL,HH]=dwt2(i,'db1');
how to convert LH band into 16 bit binary and vice versa.
First you need to define the properties of the 16 bit binary encoding that you want to use. Is it to be fixed point or is it to be floating point? Is it to be separate sigh, or "one's complement" or "two's complement" to express negative? If it is fixed point, how many bits before the implied decimal point? If it is floating point, how many bits in the exponent and how many bits in the mantissa and what is the exponent offset (which is another way of dealing with expressing negatives) ?
Good morning sir, regarding your reply; take 1 bit (MSB) for sign, 7 bits for exponent and 8 bits for the fraction.
Walter Roberson
Walter Roberson il 9 Mag 2018
Modificato: Walter Roberson il 9 Mag 2018
Okay, so what is the exponent offset? 7 bits of exponent is 0 to 127; what range of binary exponents do you want that to translate to? 2^0 to 2^127 ?
Also, do you want to use the "hidden bit" representation on the fraction?
clc; clear all;
close all;
i=imread('lena.png');
figure,imshow(i) ;
[LL,LH,HL,HH]=dwt2(i,'db1');
i have to convert LH band into binary using IEEE 754, half precision.

Accedi per commentare.

Richiesto:

il 26 Apr 2018

Commentato:

il 31 Ago 2021

Community Treasure Hunt

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

Start Hunting!

Translated by