Convert a decimal floating point integer into binary.
y = lowdisc_dec2bin ( x ) y = lowdisc_dec2bin ( x , n ) y = lowdisc_dec2bin ( x , n , m )
a nx-by-mx matrix of doubles, integer value, the decimal values to convert.
a floating point integer, the number of bits in the generated binary string. Default is n=[], which means no padding.
a floating point integer, the mode. The type of the output matrix (Default m = 1). If m=1, then a string is returned. If m = 2, then a matrix of floating point integers is returned.
a nx-by-mx matrix of strings (if m=1) or a (nx*mx)-by-nb matrix of of doubles (if m=2), where nb is the number of bits.
Given a positive integer x
this function returns its binary representation.
When required, characters '0' are added on the left in order to have components with length equal to n.
This function is vectorized, that is, takes a matrix x
as input and
returns a matrix y
on output.
If m=1, then y
is a matrix of strings, where y(i,j)
is the binary representation of x(i,j)
, for i=1,2,...,nx
and
j=1,2,...,mx
.
If m=2, then y
is a matrix of doubles, with integer values,
where the row y(k,:)
is the binary representation of x(k)
, for
k=1,2,...,nx*mx
(i.e. we can access it with a linear index).
If m=2, then all floating point integers have the same number of bits, equal to the maximum
number of digits necessary to represent the largest integer.
If m=2, and n
not equal to [], then zeros are inserted on the left
so that the matrix has n
columns.
The difference between lowdisc_dec2bin
and Scilab's dec2bin
is that lowdisc_dec2bin
can produce integers, while dec2bin
only produces strings.
// example 1 x=86 y=lowdisc_dec2bin(x) // example 2 // the binary representation of 86 is: '1010110' // its length is 7(less than n), so we add to y, // 8 times the character '0' (on the left) x=86 n=15 y=lowdisc_dec2bin(x,n) // example 3 // See that lowdisc_dec2bin is vectorized. x=[12 45 135] y=lowdisc_dec2bin(x) // See how the values are ordered in the output y x=[ 12 45 135 11 44 134 ] [mx,nx]=size(x); // See with m=1. y=lowdisc_dec2bin(x) for j = 1 : nx for i = 1 : mx disp([string(x(i,j)) ":" y(i,j)]) end end // See with m=2. y=lowdisc_dec2bin(x,[],2) for k = 1 : mx * nx disp([string(x(k)) ":" string(y(k,:))]) end // example 4 : returns integers, instead of string x=[12 45 135] y=lowdisc_dec2bin(x,[],2) // See that the result does not depend on // the orientation of x. x=[12 45 135]' y=lowdisc_dec2bin(x,[],2) // example 4 : returns integers, instead of // string and pad to 8 bits x=[12 45 135] y=lowdisc_dec2bin(x,8,2) // See that the result does not depend on // the orientation of x. x=[12 45 135]' y=lowdisc_dec2bin(x,8,2) | ![]() | ![]() |