Matlab Ruins the Double Number

1 view (last 30 days)
Erhan
Erhan on 23 May 2015
Edited: Walter Roberson on 23 May 2015
In Matlab, I need to convert a decimal number into the binary and the converted binary number into the double number.
for example:
a= 3151;
b=dec2bin(a);
b=str2double(b);
I need the answer b=110001001111 but instead of it, 1.1000e+11 will appear.
I need the lsb of the binary number and the functions below will accept only double numbers not binaries as strings.
How can I fix this issue? Thanks for your support.

Answers (1)

Guillaume
Guillaume on 23 May 2015
Edited: Guillaume on 23 May 2015
Several things:
  • b = 110001001111 is not a binary number, it's a decimal number whose binary representation would be '1100110011100100100100001001010010111'.
  • str2double converts a string representation of a decimal number into its decimal value.
  • a double can store exactly all integers up to 2^53 (see flintmax). So your b is stored exactly. The way matlab displays numbers is independent of their storage. You can change your display settings with format. Regardless, you can still get your lsb with:
lsb = mod(b, 10)
  • if all you want to get is the lsb of a number, then just ask matlab with bitget. It'll be a lot faster than the conversion to string with dec2bin + conversion to number + mod.
lsb = bitget(a, 1)

Community Treasure Hunt

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

Start Hunting!