I have a matrix of 1 column with floats between -1 and 1. Something like [0.245 -0.942 0.424 -0.543]. I want to convert each number to binary in two's complement form. I've seen dec2bin only works for positive intergers. Any suggestions?

 Réponse acceptée

John D'Errico
John D'Errico le 2 Déc 2018
Modifié(e) : John D'Errico le 2 Déc 2018

1 vote

Just extract the bits. I'll get you started, like this.
X = -0.942;
hex = sprintf('%bx',X)
hex =
'bfee24dd2f1a9fbe'
The sign bit is bit #1. Bits #2-12 are the exponent, offset by 1023. So convert the exponent bits to an 11 bit integer between 0 and 2047. bin2dec can do that. Then subtract 1023 to write it as a power of 2.
But what you are asking for is the mantissa, in twos complement form. Just extract bits #13-64. This hack (extracted from some code I once wrote) should work:
tablout = dec2bin(0:15);
tablin = transpose('0123456789abcdef');
% ismember does the work here
[~,hexind] = ismember(hex,tablin);
Xbin = tablout(hexind,:);
Xbin = reshape(Xbin',64,[])';
Xbin
Xbin =
'1011111111101110001001001101110100101111000110101001111110111110'
You need to be a little careful in representing de-normal numbers (sometimes called subnormals.) For example:
realmin/2^20
ans =
2.12199579096527e-314
is a denormal number, represented as a double. But that is not your real question, which the code I gave should help you to solve. You can recognize a denormal because the exponent you would get will be -1023.
(I need to get around to posting num2bin one day.)

4 commentaires

João Fernandes
João Fernandes le 2 Déc 2018
So i get the result as a char. I've never worked with binary values in Matlab. Should i work with Xbin as a char or is it possible use it as a binary value?
John D'Errico
John D'Errico le 3 Déc 2018
Modifié(e) : John D'Errico le 3 Déc 2018
Oh, you can use it in any you want.
Xbin
Xbin =
'1011111111101110001001001101110100101111000110101001111110111110'
For example, you can convert it to a vector of 0-1 integers.
Xbin - '0'
ans =
Columns 1 through 31
1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 0 0 0 1 0 0 1 0 0 1 1 0 1 1 1 0
Columns 32 through 62
1 0 0 1 0 1 1 1 1 0 0 0 1 1 0 1 0 1 0 0 1 1 1 1 1 1 0 1 1 1 1
Columns 63 through 64
1 0
Character is my preference, since it takes less space to display. The chars are stored as two bytes per binary digit there. So a character vector of length 64 used 128 bytes.
whos Xbin
Name Size Bytes Class Attributes
Xbin 1x64 128 char
Or I suppose you could store it as a logical vector, so one byte per.
Xlog = Xbin == '1'
Xlog =
1×64 logical array
Columns 1 through 46
1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 0 0 0 1 0 0 1 0 0 1 1 0 1 1 1 0 1 0 0 1 0 1 1 1 1 0 0 0 1 1 0
Columns 47 through 64
1 0 1 0 0 1 1 1 1 1 1 0 1 1 1 1 1 0
whos Xlog
Name Size Bytes Class Attributes
Xlog 1x64 64 logical
Just a matter of personal preference as I see it.
You can't really work in true binary as easily. For example, can you directly access the mantissa bits of a double?
bitget(1.234,1)
Error using bitget
Double inputs must have integer values in the range of ASSUMEDTYPE.
James Tursa
James Tursa le 3 Déc 2018
I don't understand what "two's complement" has to do with any of this, given that the underlying values are floating point and not integers.
Mervat Zarour
Mervat Zarour le 25 Fév 2020
was ist das Inverse von Xbin - '0' ??

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by