Two's complement representation in ARM Architecture - Time & Space Complexity
We want to understand how the time to process two's complement numbers changes as the number size grows.
Specifically, how does the cost of converting or interpreting these numbers scale with their bit length?
Analyze the time complexity of the following ARM assembly code snippet that converts a two's complement number to its positive magnitude.
CMP R0, #0 ; Compare input number with zero
BGE positive ; If number >= 0, branch to positive
MVN R1, R0 ; Otherwise, invert bits
ADD R1, R1, #1 ; Add 1 to complete two's complement negation
B done
positive:
MOV R1, R0 ; Number is positive, move as is
done:
; R1 now holds the positive magnitude
This code checks if a number is negative and if so, converts it to positive by two's complement negation.
Look for loops or repeated steps that depend on input size.
- Primary operation: Simple conditional check and bitwise operations.
- How many times: Each instruction runs once per input number, no loops.
The number of instructions executed stays the same regardless of the number's bit length.
| Input Size (bits) | Approx. Operations |
|---|---|
| 8 | 5 instructions |
| 16 | 5 instructions |
| 32 | 5 instructions |
Pattern observation: The execution steps do not increase with input size; they remain constant.
Time Complexity: O(1)
This means the time to convert or interpret a two's complement number does not grow with the number's size.
[X] Wrong: "Converting a two's complement number takes longer for bigger numbers because you have to process each bit."
[OK] Correct: The processor uses fixed-size registers and instructions that handle all bits at once, so the time stays the same no matter the number size.
Understanding that two's complement operations are constant time helps you reason about low-level data handling efficiently, a useful skill in many programming and hardware contexts.
"What if the number was stored in memory as a variable-length bit string instead of a fixed register size? How would the time complexity change?"
Practice
two's complement in ARM architecture?Solution
Step 1: Understand number representation in computers
Computers use binary to represent numbers, but need a way to show both positive and negative values.Step 2: Role of two's complement
Two's complement allows representing negative numbers by inverting bits and adding one, enabling simple arithmetic.Final Answer:
To represent both positive and negative integers in binary -> Option AQuick Check:
Two's complement = Signed integer representation [OK]
- Thinking it only stores positive numbers
- Confusing with encryption or encoding
- Assuming it converts decimal to hex
Solution
Step 1: Find binary of positive 3 in 4 bits
3 in binary is 0011.Step 2: Calculate two's complement for -3
Invert bits: 1100, then add 1: 1101.Final Answer:
1101 -> Option AQuick Check:
-3 in 4-bit two's complement = 1101 [OK]
- Using positive binary without complement
- Adding instead of inverting bits first
- Confusing bit length or dropping bits
11111010, what decimal value does it represent?Solution
Step 1: Identify if number is negative
Most significant bit is 1, so number is negative.Step 2: Find magnitude by two's complement
Invert bits: 00000101, add 1: 00000110 (6 decimal).Final Answer:
-22 -> Option BQuick Check:
11111010 two's complement = -22 [OK]
- Reading as unsigned binary (250)
- Ignoring two's complement rules
- Mistaking bit inversion step
1001. They claim it equals +9. What is the error?Solution
Step 1: Check sign from MSB
MSB is 1, so number is negative in two's complement.Step 2: Calculate decimal value
Invert bits: 0110, add 1: 0111 (7 decimal), so value is -7.Final Answer:
1001 represents -7, not +9 -> Option CQuick Check:
1001 two's complement = -7, not +9 [OK]
- Assuming binary is always positive
- Ignoring sign bit meaning
- Confusing two's complement with unsigned
00010110 (+22) and 11101010 (-22)?Solution
Step 1: Understand the numbers
00010110 is +22, 11101010 is -22 in two's complement.Step 2: Add the binary numbers
00010110 + 11101010 = 00000000 (ignoring carry out), which is 0.Final Answer:
00000000 (0) -> Option DQuick Check:
+22 + (-22) = 0 [OK]
- Expecting overflow when adding opposites
- Misreading negative number value
- Ignoring carry out in two's complement addition
Ready to prove it?
Pass the test and this pattern moves unknown
