Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
decimal fraction to binary conversion

Subject: decimal fraction to binary conversion

From: hend

Date: 2 Dec, 2011 11:02:09

Message: 1 of 6

Hello everybody;

i'm in need to a program code which can convert negative fractions into binary form ( any form ) ..
dec2bin command is not helpful in this case !

Subject: decimal fraction to binary conversion

From: ade77

Date: 2 Dec, 2011 14:49:08

Message: 2 of 6

"hend " <eng.hr2009@yahoo.com> wrote in message <jbab7h$47b$1@newscl01ah.mathworks.com>...
> Hello everybody;
>
> i'm in need to a program code which can convert negative fractions into binary form ( any form ) ..
> dec2bin command is not helpful in this case !

You can take advantage of the fact that Java is installed along with ur MATLAB. if u know basic Java, then u can do work around it.

For example to convert -5 to binary.... at the command prompt, type:
java.lang.Integer.toBinaryString(-5)

if ur Java is good enough , u will be able to find a way to handle the negative fraction and the number of bits been displayed.

Optionally, write a MATLAB program to handle what u want to do, and post it to the file exchange...... lol

Subject: decimal fraction to binary conversion

From: Jonathan

Date: 2 Dec, 2011 14:51:08

Message: 3 of 6

"hend " <eng.hr2009@yahoo.com> wrote in message <jbab7h$47b$1@newscl01ah.mathworks.com>...
> Hello everybody;
>
> i'm in need to a program code which can convert negative fractions into binary form ( any form ) ..
> dec2bin command is not helpful in this case !

There are several problems here. You will have to utilize a scheme such as Two's complement in order to represent both positive and negative numbers in binary form.

You will need to add a sign bit before the binary number representing the number. So if you want to represent +5, 5 would typically be 101, but add a sign bit and it becomes 0101. Now to represent a negative number, your sign bit will be 1 and the following numbers will be the two's complement of 5 and you end up with 1011.

You can read more here:
http://en.wikipedia.org/wiki/Two's_complement

Also, dec2bin doesn't handle fractions like you think, it returns the binary representation of only the integer portion of your input.

Subject: decimal fraction to binary conversion

From: Roger Stafford

Date: 2 Dec, 2011 21:25:09

Message: 4 of 6

"hend " <eng.hr2009@yahoo.com> wrote in message <jbab7h$47b$1@newscl01ah.mathworks.com>...
> i'm in need to a program code which can convert negative fractions into binary form ( any form ) ..
> dec2bin command is not helpful in this case !
- - - - - - - - -
  You can use 'dec2bin' if you help it out a bit. As an example, suppose you want to display 16 fractional binary digits on the number -1/(4*pi).

 d = 16;
 x = -1/(4*pi);

 b = sign(x);
 t = round(2^d*abs(x));
 [~,e] = log2(t);
 s = dec2bin(t,max(d,e));
 s = [s(1:end-d),'.',s(end-d+1:end)];
 if b<0, s = ['-',s]; end

The string s is the result. This code may not be quite the format you want but if not perhaps you can modify it appropriately.

Roger Stafford

Subject: decimal fraction to binary conversion

From: hend

Date: 3 Dec, 2011 05:44:08

Message: 5 of 6

Thanks all ..

Subject: decimal fraction to binary conversion

From: Shyamsunder

Date: 20 Dec, 2011 07:03:08

Message: 6 of 6

"Roger Stafford" wrote in message <jbbfnl$8v6$1@newscl01ah.mathworks.com>...
> "hend " <eng.hr2009@yahoo.com> wrote in message <jbab7h$47b$1@newscl01ah.mathworks.com>...
> > i'm in need to a program code which can convert negative fractions into binary form ( any form ) ..
> > dec2bin command is not helpful in this case !
> - - - - - - - - -
> You can use 'dec2bin' if you help it out a bit. As an example, suppose you want to display 16 fractional binary digits on the number -1/(4*pi).
>
> d = 16;
> x = -1/(4*pi);
>
> b = sign(x);
> t = round(2^d*abs(x));
> [~,e] = log2(t);
> s = dec2bin(t,max(d,e));
> s = [s(1:end-d),'.',s(end-d+1:end)];
> if b<0, s = ['-',s]; end
>
> The string s is the result. This code may not be quite the format you want but if not perhaps you can modify it appropriately.
>
> Roger Stafford

Hi Roger,

How will you convert the negative fractional binary number back to the decimal number?

With regards,
Shyamsunder

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us