Clear Filters
Clear Filters

converting a decimal number to its binary.

4 views (last 30 days)
H
H on 8 Oct 2018
Commented: Walter Roberson on 21 Oct 2018
Hi, I wrote a code which converts a decimal number to its binary. However, when I convert to binary it doesn’t show 52 bits in the fraction. Thus, how do I get 52 bits after the radix point? Please let me know Thank you
  4 Comments
Walter Roberson
Walter Roberson on 15 Oct 2018
You are using
num2str(x)
and breaking the displayed result up into integer and post-decimal-point parts. But num2str(x) rounds the input value instead of displaying it to full precision:
>> format long
>> num2str(753.153 + eps(753))
ans =
'753.153'
>> 753.153 + eps(753)
ans =
7.531530000000001e+02
Now, in IEEE 754, when you use up bits for the integer, fewer bits are available for the fraction, so the 10 bits used for 753 reduce the available fraction to about 42 bits. But when you break apart the .153, you do that as if it was a complete number by itself.
The integer part is not represented separately, though. 753.153 is represented as 512 times (1 plus a fraction)
So... you have two choices:
  1. redefine what it means to display the binary version of the number in such a way that your current approach works; or
  2. continue to aim for compatibility with IEEE 754 by completely reworking the way you calculate the bits (that is, divide by 2 until you get a number between 1 (inclusive) and 2 (exclusive) and then keep multiplying by 2 to figure out what bits are present.

Sign in to comment.

Answers (4)

Walter Roberson
Walter Roberson on 14 Oct 2018
if(integer=='0')
should be strcmp(integer, '0')
On the other hand, if it is that, then there is not going to be any difference between taking '0.' followed by str2, compared to taking strcat(int,'.',str2) so that entire "if" seems to be unneeded: you can just always do the "else" part.
"convert 753.153 into binary it doesn’t show 52 bits in the fraction as it should since I am using double precision"
You are not outputting a fraction. You are taking the vector of 0 and 1 and '.' values, and you are doing str2double() on it and returning that.
Consider for example if the input was decimal 5. You would find that the binary for that was '101' . You would str2double() that, which asks for the decimal number 101 to be evaluated . 101 decimal is between 64 and 128 so it would take a minimum of 7 bits binary to represent -- binary 1100101 .
Therefore, when when you use str2double() on the binary, you need more bits than you did originally.
Why not just output the character vectors?

Guillaume
Guillaume on 14 Oct 2018
The easy way to find the binary representation of a double number in matlab:
number = 753.153; %for example
dec2bin(typecast(number, 'uint64'), 64)
Now I realise this is probably homework and you're supposed to come up with your own conversion algorithm so the above is probably not acceptable
  4 Comments
Walter Roberson
Walter Roberson on 14 Oct 2018
Could just be down to the fact that the source number will have 52 bits stored (plus one hidden bit). That is, it might be acknowledging that the source is IEEE 754, but still wanting binary integer period binary fraction output.

Sign in to comment.


Stephen23
Stephen23 on 18 Oct 2018
Edited: Stephen23 on 18 Oct 2018
>> N = pi; % example number
>> S = num2hex(N)
S = 400921fb54442d18
>> X = sscanf(S,'%1x');
>> B = num2cell(dec2bin(0:15),2);
>> [B{1+X}]
ans = 0100000000001001001000011111101101010100010001000010110100011000
Could easily be written in two lines, but the intermediate steps are worth taking a look at.
  1 Comment
Guillaume
Guillaume on 18 Oct 2018
I'm not convinced that this is any faster than a simple typecast to integer followed by a dec2bin.
In my opinion it's certainly more convoluted.

Sign in to comment.


H
H on 21 Oct 2018
Edited: Walter Roberson on 21 Oct 2018
Please tell me where is the error in the first line of the following code:
f=(x+1)*(exp(-(x+1)));
g=(1/(sqrt(2*pi)))*exp(-(x.^2)/2);
hold on
grid on
axis([-2 2 -0.4 1])
x=[-2:0.01:2];
plot(x,f(x))
plot(x,g(x))
  5 Comments
Walter Roberson
Walter Roberson on 21 Oct 2018
This is still not related to converting decimal to binary and should have been a separate Question.
Walter Roberson
Walter Roberson on 21 Oct 2018
f=(x+1).*(exp(-(x+1)));
creates f as a vector, not as a function.
plot(x,f(x))
tries to use f as a function, not as an array.

Sign in to comment.

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!