CMU 15-215 videos to view
The bases of integer literals
- Bases of Java literals
- decimal
2018
- octal
0235710
- hexadecimal
0x235eA
- binary
0b0110001010111 — accepted in C++11, C#, Python and supported in
gcc extensions for C
- Integer types of Java — C also has
unsigned types
byte, 8 bits — in C99, int8_t
short, 16 bits — in C99, int16_t
int, 32 bits — in C99, int32_t
long, 64 bits — in C99, int64_t
char, 16 bits — in C99, uint16_t
- Bitwise operators of Java
~,
&, |, and ^
- Examples to do
~235
235 & 100
235 | 100
235 ^ 100
- Representation of negative numbers — two’s complement only
- Negative weight to the most significant bit
- You must know the number of bits
- Examples to do in 10-bit two’s complement
- C99 requires the use of two’s complement. Older versions of C did not.
- Shift operators of Java —
<<,
>>, >>> (not in C)
- Arithmetic vs logical right shift
- You must know the number of bits
- Examples to do in 12-bit. Assume are all signed.
235 >> 2
-235 >> 2
235 << 2
- Widening and narrowing casts
- Java defines widening and narrowing casts
- In C,
unsigned makes this difficult; however,
narrowing is fine, if input within the output range
- Overflow — signed and unsigned arithmetic
- If the result is out-of-range, overflow will happen
- Optimizing multiplication and division by constants
X<<n multiplies by 2n
— if result in range
X>>n divides by 2n
— if result in range, but
rounding is downward not toward zero
Z = X*1000 ; is probably slower than
T = X<<3 ;
Z = X<<10 - T - T - T ;