Standard Addition Function
Ignores carry output.
Implements Modular Arithmetic
Two’s Complement Addition
negative overflow: Add two negative numbers will get positive.
positive overflow: Add two positive numbers will get negative.
In principle if take two W bit numbers and multiply them together, then may need a 2w bits to represent,
because you’re potentially squaring the largest number.
Unsigned Multiplication in C
Ignores high order w bits, just like unsigned addition,
Signed Multiplication in C
Not only trunk throw away whatever high order bits, but whatever bit gets left in this position will determine whether it’s a positive or a negative result, completely irrespective of the signs of the original two operands.
Power-of-2 Multiply with Shift
Unsigned Power-of-2 Divide with Shift
Uses logical shift.
if the number can’t divisible by power-of-2, and it’s a negative number,
it should round toward zero, but actually it’s rounding toward minus infinity, it’s rounding toward a more negative number than the true number.
Before we divide by a power-of-2 do the shift, we should add a bias.
By the way, division is really really slow even on a modern computer, the shark machines it takes 30 plus Hawk cycles,
so anytime the compiler can avoid figure out a trick that does it with shifting and tweaking things around it will.
Negate a Number
Flip all the bits, and add 1 to that.
Why Should I Use Unsigned?
Do Use When Performing Modular Arithmetic
Do Use When Using Bits to Represent Sets
Logical right shift, no sign extension.
How Should I Use Unsigned?
A wrong way to Use unsigned as loop index
Byte-Oriented Memory Organization
Programs refer to data by address
Conceptually, envision it as a very large array of bytes(In reality, it’s not, but we can think of it that way)
An address is like an index into that array, and a pointer variable stores an address.
A trick to get an approximate value
, what it means is that 10 bits worth of number is about the same as three decimal digits.
so, , ,
Any given computer has a “Word Size”
Nominal size of integer-valued data a,d of addresses.
Some machines used 32 bits (4 bytes) as word size, Limits addresses to 4GB.
Increasingly, machines have 64-bit word size.
Potentially, could have 18 PB(petabytes) of addressable memory
If we compile a program using GCC is the standard compiler, we can specify either we want it to be 64 bit code or 32 bit code as a flag, and it will actually generate two different kinds of object code as a result.
The hardware itself doesn’t necessarily define what the word size is, it’s a combination of the hardware and the compiler that determines.
Word-Oriented Memory Organization
The memory itself is a series of bytes, but we can group those into blocks of words of different word sizes, the way is usually by assuming that the address of the word is the lowest value address in it.
show_bytes Execution Example
int a = 15312;
int a = 15213; 00003b6d
Strings in C
Represented by array of characters, each character encoding in ASCII format.
Standard 7-bit encoding of character set
String should be null-terminated, final character = 0
- 1. Unsigned Addition
- 2. Two’s Complement Addition
- 3. Multiplication
- 4. Unsigned Power-of-2 Divide with Shift
- 5. Negate a Number
- 6. Why Should I Use Unsigned?
- 7. How Should I Use Unsigned?
- 8. Byte-Oriented Memory Organization
- 9. Machine Words
- 10. Word-Oriented Memory Organization
- 11. show_bytes Execution Example
- 12. Representing Strings