# How we can convert negative float value into binary in matlab? Is there any direct command for this?

22 views (last 30 days)

Show older comments

##### 0 Comments

### Accepted Answer

Roger Stafford
on 1 Sep 2013

This is a function I wrote for my own use long ago. Perhaps you can make use of it or it can give you some ideas. It converts a single scalar 'double' to a string of 53 binary digits, including a binary point, a sign, and an exponent of 2. This representation is precise, giving exactly what is contained in the number as stored in its internal IEEE 754 format.

%%%%%%%%%%%%%%%%%%%%%

function s = binstr(x)

if ~finite(x)|(length(x)~=1), error('x must be a finite scalar.'),end

b = (x<0); x = abs(x);

s = zeros(1,53);

[f,e] = log2(x);

for i = 1:53

f = 2*f;

d = floor(f);

f = f - d;

s(i) = d+48;

end

s = ['0.' s sprintf('*2^(%d)',e)];

if b, s = ['-' s]; end

s = char(s);

return

%%%%%%%%%%%%%%%%%%%%%

##### 0 Comments

### More Answers (4)

rifat
on 1 Sep 2013

Edited: rifat
on 1 Sep 2013

You can consider two things.

1. consider changing the quantization of the number and convert the numbers into int16 format which are, actually, 16 bit signed integers. Since, they are in signed binary format, you can fit in negative numbers as well. And floating numbers will be removed due to quantization into integer format.

you can change the quantization by doing as follows:

val=val*(2^15-1)/max(abs(val(:)));

val=int16(val);

2. You can directly change the floating negative numbers into binary as per IEEE standard. You can find it in any appropriate book. Also, you can take a look at this link : http://stackoverflow.com/questions/13436486/negative-floating-point-to-binary-conversion

##### 0 Comments

Walter Roberson
on 1 Sep 2013

##### 0 Comments

Kartik Sewani
on 21 Feb 2017

Edited: Kartik Sewani
on 23 Feb 2017

Can someone help me with this question? Using Matlab, write a code to determine the floating point binary representation for the following numbers in single precision and double precision? Can someone tell me the bit order .

a)356.75 b)-11.5

##### 2 Comments

Walter Roberson
on 23 Feb 2017

"Can someone tell me the bit order"

All numeric representations in MATLAB are Most Significant Bit to the "left" within its byte. However, the byte order is not always "Most Significant Byte" to the "left".

Data transmitted by RS232 is always sent Least Significant Bit first; however it is reassembled on the other end into MSB first.

There are some communications toolbox functions that are bit-oriented and that tend to work with Least Significant Bit to the "left".

DERNI BRAHIM
on 9 Oct 2017

Edited: Walter Roberson
on 10 Oct 2017

you can use this script matlab

for example if we are a vector a .if we want to convert elements to binary and make result in new vector with one colon use this:

a=[1 2 3 4;-2 -4 3 4;7 8 9 4];

[c,v]=size(a);%depth of vector h

n3=c*v;

word_len=5;% length of binary word

data = reshape(a', n3, 1);

databin= fi(data,1,5);

h=bin(databin)

##### 1 Comment

Walter Roberson
on 10 Oct 2017

In the code given, the resolution of the output will depend upon the range of the inputs. fi() will choose the meaning of the representation automatically. For example,

data = (-3:.1:-2) .';

databin1 = fi(data,1,5)

bin(databin1)

databin2 = fi(data*2,1,5)

bin(databin2)

The range of values being represented is different, which the fi object knows, but the bin() output is the same. So you have bits of output, but you don't know what they mean.

You can pass options to fi() to choose a specific representation -- but like I described in my answer, that requires that you choose a representation, because there are a lot of possibilities.

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!