lecture 01

args.cbool_fun.cLecture01.pdf

lecture 02 Bits and Bytes; Integer Representations

airline.cLecture02.pdf
![1BO2GXET{T]ECOZVB}F_~H.png
G68ZU}QW4VHMMH(40S6LB]7.png
~6RB@JX@F13HPZ(VNNLE9FO.png
T`C(){WBQYTF2PMR$`AX{B2.png![N80F{3JRR4{R8P`V4PA5XW.png
![1KA4VPT)H{6QVS]T0GZ0HU.png
![K`F$PS96B8TY9LL32MIKCN.png

lecture 03 Bits and Bytes; Bitwise Operators

absolute_value.cabsolute_value_soln.cbits_playground.cbitvectors_masks.cbitvectors_masks_soln.ccolor_wheel.ccolor_wheel_soln.cis_power_of_2.cis_power_of_2_soln.cLecture03.pdflive_session.c
![EWZSXJLL[)XN(U)6P7YLPE.png

image.png
There are two kinds of right shifts, depending on the value and type you are
shifting:
Logical Right Shift: fill new high-order bits with 0s.
Arithmetic Right Shift: fill new high-order bits with the most-significant bit.
Unsigned numbers are right-shifted using Logical Right Shift.
Signed numbers are right-shifted using Arithmetic Right Shift.
This way, the sign of the number (if applicable) is preserved!
However, almost all compilers/machines use arithmetic, and you can most likely assume this. 几乎所有编译器都默认算术右移。
image.png

位操作实现绝对值函数

  1. /* Bitwise version of absolute value with no conditionals or multiplication */
  2. int absolute_value_bitwise(int value) {
  3. int sign = value >> (sizeof(int) * CHAR_BIT - 1);
  4. /*
  5. * When value is:
  6. * Nonnegative, value has 0 in MSB (sign bit), sign is all 0s (0)
  7. * Negative, value has 1 in MSB, sign is all 1s (-1)
  8. */
  9. return (value ^ sign) - sign;
  10. /*
  11. * When val is nonnegative:
  12. * -- sign is 0. XOR with 0 keeps the same value (i.e. 1 ^ 0 == 1, 0 ^ 0 == 0)
  13. * -- (value ^ sign) == value. value - sign = value - 0 = value
  14. *
  15. * When value is negative:
  16. * -- sign bit pattern is all 1s. XOR with 1 flips bit (i.e. 0 ^ 1 == 1, 1 ^ 1 == 0)
  17. * -- (value ^ sign) == ~value
  18. * -- sign has numeric value of -1. y - sign == y + 1
  19. * -- (value ^ sign) - sign = ~value - sign = ~value + 1, which is the two's complement formula for -val
  20. */
  21. }

位操作实现判断一个数是否是2的n次方

  1. /* This function uses bitwise operators to return whether or not
  2. * this number is a power of 2. */
  3. bool is_power_of_2(unsigned int value) {
  4. // TODO
  5. return value != 0 && (value & (value - 1)) == 0;
  6. }

lecture 04

diamond.cdiamond_soln.cLecture04.pdflive_session.c

string

strlen函数计算字符数组的长度,不包括\0,时间复杂度为O(n)。
image.png
image.png
image.png
image.png
image.png
image.png
image.png
image.png
Both strcat and strncat remove the old ‘\0’ and add a new one at the end.
image.png
image.png

image.png
image.png
image.png**
An array name (and a string name, by extension) is the address of the first element.
image.png
image.png