recategorized by
2,498 views
19 votes
19 votes

The following is an incomplete Pascal function to convert a given decimal integer (in the range $-8$ to $+7$) into a binary integer in $2$’s complement representation. Determine the expressions $A, B, C$ that complete program.

function TWOSCOMP (N:integer):integer;  
    var  
    REM, EXPONENT:integer;  
    BINARY :integer;  
    begin  
        if(N>=-8) and (N<=+7) then   
        begin
            if N<0 then  
                N:=A;  
            BINARY:=0;
            EXPONENT:=1;
            while N<>0 do
            begin
                REM:=N mod 2;
                BINARY:=BINARY + B*EXPONENT;
                EXPONENT:=EXPONENT*10;
                N:=C 
            end
            TWOSCOMP:=BINARY
        end  
    end;
recategorized by

2 Answers

Best answer
7 votes
7 votes
$A=16+N, ($for $N = -1, A = 15$ which is the largest value, for $N = -8, A = 8)$

$B=\text{REM}$

$C=N/2$
edited by
3 votes
3 votes
for A:

Since negative number can come as (-8,-7,-6.....-1)

and 2's complement number is used for n=4.

so corresponding binary representation will be: N+2^4 = N+16.

for B:

Concatenation is used here BINARY : BINARY + B * EXPONENT

so whatever remainder will come in above stage which is REM will be used here so B=REM

for C:

here value of N will be divided by 2 and remainder will be assign in N (Updation of N is done here)

so C= N/2.
edited by

Related questions

31 votes
31 votes
6 answers
1
Kathleen asked Oct 8, 2014
18,278 views
The number of $1$'s in the binary representation of $(3\ast4096 + 15\ast256 + 5\ast16 + 3)$ are:$8$$9$$10$$12$
7 votes
7 votes
1 answer
2
go_editor asked Feb 12, 2018
2,513 views
What is the equivalent minimal Boolean expression (in sum of products form) for the Karnaugh map given below?
14 votes
14 votes
2 answers
3
Kathleen asked Oct 8, 2014
2,531 views
Implement a circuit having the following output expression using an inverter and a nand gate $$Z=\overline{A} + \overline{B} +C$$
29 votes
29 votes
3 answers
4