Types & Operators CAS CS210 Ying Ye Boston University Outline Endianness Bitwise operators Arithmetic operators Data lab assignment Endianness Endianness is the same as byte order, refering to how bytes are ordered within a single large data item( e.g. int, short, long). Little endian: stores the least significant byte first Big endian: stores the most significant byte first Endianness int x = 0x44332211 0 4 44 33 22 11 big endian 0 4 11 22 little endian 33 44 Bitwise operators wget http://cs-people.bu.edu/yingy/bitwise.c Open the file and replace all /*TODO*/ with the expected expressions Compile and run Bitwise operators Output Bitwise operators Bit mask wget http://cs-people.bu.edu/yingy/mask.c replace all /*TODO*/ with the expected expressions Output Arithmetic operators In just one statement, make a == 2, b == 3 wget http://cs-people.bu.edu/yingy/math.c replace the /*TODO*/ with the expected statement A possible answer: a = ++b, b++; Data lab assignment Example: For a one byte unsigned variable, calculate the number of ones in its binary representation. (e.g. variable = 0x03, number of ones: 2) int Count(unsigned char v) { int num = 0; while(v){ num += v & 0x01; v >>= 1; } return num; } Data lab assignment Challenge! Can you find an algorithm as fast as possible to solve this counting problem. Data lab assignment Faster int Count(unsigned char v) { int num = 0; while(v){ Can we solve it even faster than this code? v &= (v - 1); num++; Maybe...... } return num; } Based on the fact: for a variable v = xxxx1000(x can be 1 or 0), v - 1 == xxxx0111, so v & (v - 1) == xxxx0000, the '1' in the rightmost is erased. The number of this operation needed to make v == 0 is the number of '1's in v.