arithmetic
Integer types
int8_t a; int16_t b; int32_t c; int64_t d;
uint8_t x; uint16_t y; uint32_t z; uint64_t w;
Booleans are int
C99: #include <stdbool.h>
Integer representation
- by default integers are signed
- Expression evaluation: When mixing unsigned and signed in single expression, signed values implicitly cast to unsigned.
- Negate:
-x==~x+1 - Sign extension:
- unsigned: add zeros
- signed: make copies of sign bit
- Addition: TAdd and UAdd have identical behavior
- UAdd (Unsigned Addition) :
- TAdd (Two’s complement addition)
- Multiplication:
- UMult:
- forms a commutative ring with
- shortcuts if one number is known:
int mul12(int x) { return x*12; }->t <- x+x*2 return t << 2;
- UMult:
- Division: rounded towards 0, (making sure distributivity with division works)
- divided by :
- Unsigned: logical right shift bits
- signed: can be computed by
- divided by :