lecture 01
lecture 02 Bits and Bytes; Integer Representations
airline.cLecture02.pdf
![1BO2GXET{T]ECOZVB}F_~H.png![G68ZU}QW4VHMMH(40S6LB]7.png](/uploads/projects/shengruozhimu@lkgzpq/be1f15278a6532b3e9838e4c40b507b8.png)

![N80F{3JRR4{R8P`V4PA5XW.png
![1KA4VPT)H{6QVS]T0GZ0HU.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. 几乎所有编译器都默认算术右移。
位操作实现绝对值函数
/* Bitwise version of absolute value with no conditionals or multiplication */int absolute_value_bitwise(int value) {int sign = value >> (sizeof(int) * CHAR_BIT - 1);/** When value is:* Nonnegative, value has 0 in MSB (sign bit), sign is all 0s (0)* Negative, value has 1 in MSB, sign is all 1s (-1)*/return (value ^ sign) - sign;/** When val is nonnegative:* -- sign is 0. XOR with 0 keeps the same value (i.e. 1 ^ 0 == 1, 0 ^ 0 == 0)* -- (value ^ sign) == value. value - sign = value - 0 = value** When value is negative:* -- sign bit pattern is all 1s. XOR with 1 flips bit (i.e. 0 ^ 1 == 1, 1 ^ 1 == 0)* -- (value ^ sign) == ~value* -- sign has numeric value of -1. y - sign == y + 1* -- (value ^ sign) - sign = ~value - sign = ~value + 1, which is the two's complement formula for -val*/}
位操作实现判断一个数是否是2的n次方
/* This function uses bitwise operators to return whether or not* this number is a power of 2. */bool is_power_of_2(unsigned int value) {// TODOreturn value != 0 && (value & (value - 1)) == 0;}
lecture 04
diamond.cdiamond_soln.cLecture04.pdflive_session.c
string
strlen函数计算字符数组的长度,不包括\0,时间复杂度为O(n)。







Both strcat and strncat remove the old ‘\0’ and add a new one at the end.



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

