Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binary representation of a number in Matlab

Is there a Matlab function that returns the binary representation of a float number?

like image 883
Academia Avatar asked Dec 20 '11 04:12

Academia


People also ask

How do you represent a number in binary?

To convert integer to binary, start with the integer in question and divide it by 2 keeping notice of the quotient and the remainder. Continue dividing the quotient by 2 until you get a quotient of zero. Then just write out the remainders in the reverse order. Here is an example of such conversion using the integer 12.

How do you represent 6 in binary?

6 in binary is 110. Unlike the decimal number system where we use the digits 0 to 9 to represent a number, in a binary system, we use only 2 digits that are 0 and 1 (bits).

What is de2bi function in MATLAB?

b = de2bi( d ) converts a nonnegative decimal integer d to a binary row vector. If d is a vector, the output b is a matrix in which each row is the binary form of the corresponding element in d . b = de2bi( d , n ) has an output with n columns.

What is the binary representation of the integer?

Binary numbers are usually represented with just two digits — 0 and 1 — cor- responding to the off and on states of the internal hardware. It takes quite a few more digits to represent a number in binary than in decimal, but any number that can be expressed in one can be converted to the other.


3 Answers

In Matlab, one can use Java JDK functions.

The short answer for converting float (single precision 32-bit number) in Matlab to a binary string representation might be:

flt=3.14
import java.lang.Integer java.lang.Float;
Integer.toBinaryString(Float.floatToIntBits(flt))

The long answer: conversion of float (single precision 32-bit number) in Matlab to a binary string representation

function out=float2binstring(flt)
% converts a float number to binary in matlab according to IEEE754
%
% Usage:
% float2binstring(-3.14)
%
% http://www.h-schmidt.net/FloatApplet/IEEE754.html
%
% Limitations:
% Rounding errors: Not every decimal number can be expressed exactly as a     floating
% point number. This can be seen when entering "0.1" and examining its binary     representation which is either slightly smaller or larger, depending on the last bit. 
%
% Andrej Mosat, [email protected]
% 03/2012
% v0.0
% License: GNU GPL
%
% See also: BINSTRING2FLOAT


% this is a trick to use java JDK should be installed, tested on Matlab R2010
import java.lang.Integer java.lang.Float;

if ( ~isnumeric(flt) )
    error('input must be a number');
end

out=Integer.toBinaryString(Float.floatToIntBits(flt));
end

And a conversion of binary string to float with a little overhead:

function out=binstring2float(binstr)
    % converts a binary string to float number according to IEEE754
    %
    % Usage:
    % binstring2float('11000000010010001111010111000011')
    %   -3.14
    %
    % 
    % http://www.h-schmidt.net/FloatApplet/IEEE754.html
    %
    % Limitations:
    % Rounding errors: Not every decimal number can be expressed exactly as a floating
    % point number. This can be seen when entering "0.1" and examining its binary representation which is either slightly smaller or larger, depending on the last bit. 
    %
    % Andrej Mosat, [email protected]
    % 03/2012
    % v0.0
    % License: GNU GPL
    %
    % See also: FLOAT2BINSTRING

    import java.lang.Long java.lang.Float;

    if isequal(class(binstr), 'java.lang.String')
            binstr=char(binstr);
    end

    if ( ~isstr(binstr) )
        error('input must be a binary string');                                             
    end
    % Error handling for binary strings should be added here

    % the sign is negative

    if binstr(2)=='1'
        binstr(2)='';
        isnegative=1;
    else
        isnegative=0;
    end

    out=Float.intBitsToFloat( Long.parseLong(  binstr  , 2) );
    if isnegative
     out=-out;
    end

end
like image 52
mosi Avatar answered Oct 06 '22 01:10

mosi


It looks like you can use num2hex to convert a floating point into a hex string.

like image 31
bobbymcr Avatar answered Oct 05 '22 23:10

bobbymcr


Check this FileExchange submission:

Floating number conversion to binary and vice-versa

like image 30
yuk Avatar answered Oct 06 '22 01:10

yuk