← Back to challenges

8 Bit Arithmetic

JavaScriptHardbit_operationsnumbers

Instructions

You will be given a simple string expression representing an addition or subtraction in 8-bit 2's complement arithmetic. Write a function that returns the result in base 10 followed by a binary representation. If any of the values are outside the range of 8-bit 2's complement, return "Overflow".

Examples

eightBit("3 + 12") ➞ [15, "11 + 1100 = 1111"]

eightBit("3 - 12") ➞ [-9, "11 - 1100 = 11110111"]

eightBit("-18 - 6") ➞ [-24, "11101110 - 110 = 11101000"]

eightBit("65 + 70") ➞ "Overflow"

eightBit("-127 + 127") ➞ [0, "10000001 + 1111111 = 0"]

Notes

Numbers in 8-bit 2's complement notation can range from -128 to 127. The eighth (leftmost) bit signifies a negative number.

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.