I'm writing a user-defined function to convert integers to binary. The largest number that could be converted with the function should be a binary number with 16 1 s. If a larger number is entered as d, the function should display an error message. With my code, I'm trying to add the numbers 0 or 1 to my vector x based on the remainder, then I want to reverse my final vector to display a number in binary. Here's what I have:
function [b] = bina(d)
% Bina is a function that converts integers to binary
x = [];
y = 2;
in = d/2;
if d >=(2^16 -1)
fprintf('This number is too big')
else
while in > 1
if in >= 1
r = rem(in,y);
x = [x r]
end
end
end
end
de2bi: mathworks.com/help/comm/ref/de2bi.htmldec2binin octave.inin the while loop, that will be an infinite loop.