data lab
Confusion
logical operators
||, &&, !, which treat any nonzero argument as representing TRUE and argument 0 as representing FALSE. And then they return either 1 or 0, indicating a result of either TRUE or FALSE, respectively.A second important distinction between the logical operators ‘&&’ and ‘||’
versus their bit-level counterparts ‘&’ and ‘|’ is that the logical operators do not
evaluate their second argument if the result of the expression can be determined
by evaluating the first argument.
1 | //Rounding toward zero |
unsigned and two’s complement
1 |
|
Fatal
Some possibly nonintuitive behavior arises due to C’s handing of expressions containing combinations of signed and unsigned quantities.
**When an operation is preformed where one operand is signed and the other is unsigned, C implicitly casts the signed argument to unsigned. **
As an example, Since sizeof would return a size_t type, the int type of maxbytes will be implicitly casts to be unsinged. Hence no matter how small the maxbytes is, the condition all way is true and call the function memcpy().
1 | void copy_int(int val, void *buf, int maxbytes) { |
So we can rewrite the condition to be maxbytes >= sizeof(val)
Trick
x -> -x
~x + 1
mask
We often use a mask to extract the special bits in a value. We create a mask, its corresponding bits are 1 and the other bits are zeros.

1 | int any_odd_one(unsigned x) |
logical and arithmetic right shift
1 | int lower_one_mask(int n){ |
Representations of number In C
Decimal
int x=10;Octal:
int x = 012;Hexadecimal
int x = 0xa;Binary
int x = 0b1010
