Binary & Int representations | Bitwise Operations Flashcards
voltage for binary numbers
0: 0V
1: 3.3V
How many bit patters can an n-bit register hold?
2^n bit patters
Range of unsigned integers and how are they encoded?
Range: 0 to (2^n)-1
Encoded using binary numbers
Range of signed integers and how are they encoded?
Range: -(2^n-1) to (2^n-1)-1
Encoded using two’s complement
How is a number negated?
- Take one’s complement (flip bits)
- Add 1 to the result
How to distinguish a +/- signed int?
The leftmost bit is the signed bit. 0 = + and 1 = -
Do we use sign-magnitude and one’s complement of signed integers?
No, they are awkward to handle in hardware, they have +0 and -0. Rarely used today.
What are hex numbers commonly used for?
Shorthands for denoting bit patterns
What data model does Linux on ARMv8 in AArch 64 use?
LP64 data model
Long ints and
Pointers are
64 bits long
What are the 5 A64 keywords, their sizes in bits and corresponding C keyword?
- Byte, 8 bits, char
- Halfword, 16 bits, short int
- Word, 32 bits, int
- Doubleword, 64 bits, long int / void
- Quadword, 128, N/A
How to define an unsigned integer in C?
unsigned int x;
or for char
unsigned char x;
What do bitwise logical instructions do?
Manipulate 1 or more bits in a register
what’s it called when these two are anded together and the result is all 0’s
1010
0000
———
0000
A bitmask “masks out” these bits
Check if bit 3 is set in x20
uses ands, check notes
set bits 4&5 in x20
uses orr, check notes
orr doesn’t need an s
What does bit clear mean and what is its opcode?
AND NOT, bic
Clear bits 2-5 in x20
use bic (bitclear), check notes
Opcode for exclusive or
eor
Opcode for OR NOT
orn
NOT opcode and what is it an alias for?
mvn xd, xm
(move not)
Alias for: orn xd, xzr, xm
Exclusive or not opcode
eon
Explain rotate right and what is its opcode?
Bits shifted out the right are inserted on the left.
ror
Explain Signed Extend Byte and what is its opcode?
Sign-extends bit 7 to bits 8-31
stxb
What are all the sign-extend operations, what do they do and what registers do they use?
- Signed Extend Byte: extends bits 8-31, uses w
- Signed Extend Halfword: extends bits 15 to bits 16-31, uses w
- Signed Extend Word: extends bits 31 to bits 32-64, uses x
What are the unsigned extend operations?
- Unsigned extend byte: uxtb, take something that’s 8 bits wide, extend it to 32
- Unsigned extend halfword: uxth, takes something that’s 16 bits wide and extend to 32
- Unsigned extend word: uxtw, takes something that’s 32 bits wide, extends it to 64
What does modulus arithmetic do?
- Constrains numbers to the range 0 to M-1 where M is the modulus. - CPUs normally do modulus arithmetic.
- If n is the size in bits, M=2^n
- Carry out is ignored
On a 4-bit CPU, what does 9+8 give?
(9+8) mod 16 = 1
What can adds or subs be used for and what new flag gets set compared to ands?
Can be used for extended precision arithmetic and carry flag is set.
Full adder circuit diagram
Check notes
How are multi-bit numbers summed in hardware?
Using multiple full adder circuits (1 for each bit). See diagram in notes
How is subtraction done in the ALU?
Negating the subtrahend and then adding. In 7-5, 5 is the subtrahend. The carry out is also ignored here.
Subtract 7-5 in binary
2 but check notes on how it’s done
What flags for signed branch instructions use?
N,Z,V
How does the V flag work for subtraction? Also how to subtract -8 - 5?
V=true if xd=0, xn=1, xm=1 or xd=1, xn=0, xm=0
For -8-5, negate both binary representations of 8 and 5. Add.
Difference between unsigned and signed arithmetic
Both use the same registers and hardware, but in unsigned, one interprets the data as unsigned numbers. A carry out indicates an overflow
Chart for unsigned branching conditions
We use C instead of V
- eq, equal, ==, Z=1
- ne, not equal, !=, Z=0
- hi, higher than, >, Z==0 && C==1
- hs, higher than or same, >=, C==1
- lo, lower than, <, C==0
- ls, lower than or same, <=, !(Z==0 && C==1)
What does an if statement use for branching conditions?
Logical complement
How is binary multiplication done?
- Repeated for every bit in the source register, each step consists of 2 sub-steps:
1. a conditional addition
2. an asr - If the original multiplier is negative, an extra step is needed: subtract the multiplicand from the high-order part of the result (from the product register)
Where is the product of binary multiplication put?
In 2 concatenated registers