Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

decimal fraction to binary conversion

559 views
Skip to first unread message

hend

unread,
Dec 2, 2011, 6:02:09 AM12/2/11
to
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 !

ade77

unread,
Dec 2, 2011, 9:49:08 AM12/2/11
to
"hend " <eng.h...@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

Jonathan

unread,
Dec 2, 2011, 9:51:08 AM12/2/11
to
"hend " <eng.h...@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.

Roger Stafford

unread,
Dec 2, 2011, 4:25:09 PM12/2/11
to
"hend " <eng.h...@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

hend

unread,
Dec 3, 2011, 12:44:08 AM12/3/11
to
Thanks all ..

Shyamsunder

unread,
Dec 20, 2011, 2:03:08 AM12/20/11
to
"Roger Stafford" wrote in message <jbbfnl$8v6$1...@newscl01ah.mathworks.com>...
Hi Roger,

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

With regards,
Shyamsunder
0 new messages