Click here to Skip to main content
15,891,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need help with coding in matlab, how to code binary. I have no idea how to start really, So please, if anyone can explain how to do it, in coding that will be very helpful,

Decimal binary
0 000
1 001
2 010
3 011
4 100
5 101
6 110
7 111

[Revision2 changed spelling, and corrected binary representation of decimal numbers; daveauld]
Posted
Updated 13-Mar-10 19:35pm
v2

Here you go:
clear
clc
num=input('Enter a  positive number: ');
num2=num;
while (num<0)
   clc
   fprintf('Invalid entry!\n');
   a=input('Enter a positive number: ');
end
a=1;
c=num;
d='';
while (c>=1)
   b=rem(c,2);
   c=c/2;
   c=floor(c);
   d=[d char((b)+'0')];
   %a=a*10;
end
lsb=rem(num,2);
msb=b;
   
fprintf('\n\n%g is written %s in binary form.\n\n',num2,d)
fprintf('The least-significant bit is %g\n\n',lsb)
fprintf('The most-significant bit is %g\n\n',msb)
 
Share this answer
 
thank you very much :) you were very helpful
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900