Thursday, 16 December 2010

Algorithm for To convert the given binary number to its 2’s complement

Description:


In this program in the given binary number  first covert the numbers 0 to1 and 1 to 0. And finally add the 1 to the converted number. Then we will get the 2’s complement number.

main program
Step 1: Start
Step 2: declare the subprogram “complement(char *a)” Step 3:  initialize the variable i

Step 4: read the binary number
Step 5: perform the loop operation. if it is true then follows. if not go to step 7

i) for(i=0;a[i]!=’\0’;i++)
ii) if(a[i]!=’0’&&a[i]!=’1’) then displayed the number is not valid. enter the correct number.
iii) Exit the loop
Step 6: call sub program ‘complement(a)’
Step 7: stop

Sub program:


Step 1: initialize the variable I,c=0,a[16]
Step 2: 1=strlen(a)
Step 3: perform the loop operation. if it is true then follows. if not goto i)for(i=l-1;i>=0;i--)
ii)if(a[i]==’0’) then b[i]=’1’ else
iii)b[i]=’0’
Step 4: for(i=l-1;i>=0;i--) is true
I. if(i==l-1) then
II. if(b[i]==’0’) then b[i]=’1’ else
III. b[i]=’0’,c=1 if not goto step 5

Step 5: if(c==1&&b[i]==’0’) is true then
  b[i]=’1’, c=0 if not goto Step 6

Step6: if(c==1&&b[i]==’1’) then b[i]=’0’,c=1
Step 7: displayed b[l]=’\0’

Step 8: print b and return to main program

No comments:

Post a Comment