MUL2COM - Binary multiplication
In this problem, you have to multiply two n-bit binary numbers in 2s complement form. The result should be also a n-bit binary number in 2s complement form. In case there is an arithmetic overflow, you program should be able to detect it.
For your information, the 2s complement form of -x is the number 2^n-x. A n-bit 2s complement number ranges from -2n-1 to 2n-1-1.
There are multiple test cases (no more than 40). For each test case, there are three input lines. The first line contains n (0 ≤ n ≤ 1024). n=0 signals the end of the input. Otherwise, the second and third lines contain the two n-bit binary numbers.
For each test case, output "overflow" if there is an arithmetic overflow. Otherwise, print the result in 2s complement form.
Input 3 110 011 4 0011 1110 0 Output overflow 1010
@Duc can you please check my last submission I think it's correct but WA here :(
easy one.... AC in first attempt! :D
what will be the output for :
please show some other test cases ?Last edit: 2013-06-20 06:30:08
@Tushar Makkar: Because 1110 in 2s complement corresponds to -2 in decimal (not 14!!!)
Tushar Makkar (Retired):
can anybody please explain me the 2nd test case ? Why is it not giving overflow although the answer is far beyond the range specified ?
I don't understand the question. The input values are in 2s if you are asking that.
do we have to take ....2s of both numbers while multiplying???
This is how I remember twos complement form:
the 2s complement form of -x is the number 2^n-x.
|Cluster:||Cube (Intel G860)|
|Languages:||ADA95 ASM32 BASH BF C CSHARP CPP C99 CLPS LISP clisp LISP sbcl D FORTRAN HASK ICON ICK JAVA LUA NEM NICE OCAML PAS-GPC PAS-FPC PERL PHP PIKE PRLG-swi PYTHON RUBY SCM guile SCM qobi ST WHITESPACE|