1. Binary conversion
1). 10 binary to N binary. Method: ( even remove the residual ).
2). The n binary is converted to 10 binary. Method: ( weighted sum method ): The number on the current bit is multiplied by the base number minus 1.
Cases:
0b1111----1* 2^3 + 1*2^2 +1 * 2^1 +1*2^0 = 15;
038-----3*16^1 +8*16^0 = 56;
3). n-binary converts n-binary.
For example: binary---16 binary
1010 0011----A 3
1111 0011----F 3
int a = 010;//8 int b = 0x10;//16 in printf ("%d\n", a); printf ("%d\n", b); /** How to Output binary: %d ------ decimal %0x------hex %o ---- octal */
2. Bitwise operators
The bitwise and & int a = 5 & 7;//5 101 7 111 are the same as 1, or 1. otherwise 0., used to give a one to do clear 0 operation. printf ("%d\n", a);//101//Bitwise OR | int B = 5 | 7;//101 111 is the same as 0, then 0, otherwise 1., which is often used to keep a certain state. printf ("%d\n", b);//111//bitwise XOR ^ int c = 5 ^ 7; printf ("%d\n", c);//101 111 010, same as 0, different 1//bitwise NON ~ char D = ~ 7;//each bit is reversed. printf ("%d\n", D); 7 111-1000-8//Negative complement operation: absolute value inversion plus 1 sign bit 1 is equivalent to 0 equals +//If it is a signed number, the binary highest bit represents the sign bit, 1 is negative, and 0 represents a positive. The data is stored in memory in the form of complement, the complement of positive number is the positive value itself, the complement of negative number is absolute negation plus one. A byte represents a eight-bit binary number. /** * Data Type value range: * unsigned: * Char 0 ~ 2^8-1//0-255 * Short 0 ~ 2^16-1 * int 0 ~ 2^32-1 * Signed: * Char 11111111 signed, highest bit is sign bit,-2^7 ~2^7-1-128-127 (positive number includes 0, that is) * short-2^15 ~ 2^15-1 * int -2^31 ~ 2^31-1 */char F = 255;//11111111 absolute value 01111111 negation 100000000 plus 1 100000001 i.e.-1, negative numbers are stored in the complement of printf ("% D\n ", f);//-1 unsigned char e = 255; printf ("%d\n", e);//out of range overflow, i.e. 0&NBsp Shift left << unsigned char g = 1; printf ("%d\n", G < < 3)//multiply by 2 3 times//8 //Right Shift >> printf ("%d\n", 255 >> 2)////Except for 2 of the 2-time Square//63examples:
1. Swap the 100 high four bits with the fourth bit. 100:0110 0100
unsigned char number = 0b01100100; 1. Move left four-bit unsigned char = number << 4;//01000000 //2. Move four-bit unsigned char right = number >> 4; 00000110 //3. Bitwise OR unsigned char result = left | right;//01000110 printf ("%d\n", result);//70
2. Swap 10010010 odd and even bits
unsigned char num = 0b10010010;//146 //1. Move one bit to the left to get the odd digit unsigned char left1 = num << 1;//00100100, because it's char, when Move left, value =146*2 overflow. But the transposition int is OK. Char type range, -128-127 printf ("shift left: num =%D,LEFT1 =%d\n", num, left1); 2. Keep the even number to clear the odd digit 0. 00100000 unsigned char l = left1 & 0b10101010;//odd digit 0, that is, the odd digit is 0.& with 1 that is 1 //3. Move right one get unsigned char right1 = num >> 1;//01001001 //printf ("shift right: num =%d,right1 =%d\n", num, right1); //4. Preserving odd digits, speaking even digits 0.01000001 unsigned char r = right1 & 0b01010101; //5. Bitwise OR unsigned char s = R | l; printf ("%d\n", s);
2. Two number exchange, do not use a third variable (enterprise-class approach)
int m = ten, n = 5; m = m ^ n; n = m ^ n; m = m ^ n; printf ("%d%d\n", M, N);
4. Stack memory allocation principle
<span style= "color: #000000;" >/** * Stack memory allocation principle * principle: from high to low allocation, from low to high access. * Address: A number of memory units *//The array name represents the first address of the array , which is the address of the first element in the array. is a constant address. int m1[5] = {1, 2, 3, 4, 5}; printf ("%p\n", M1); 0X7FFF5FBFF7F0 Results printf ("%p\n", &m1[0]);//0x7fff5fbff7f0 printf ("%p\n", &m1[1]);// 0x7fff5fbff7f4 printf ("%p\n", &m1[2]);//0x7fff5fbff7f8 printf ("%p\n", &m1[3]);//0X7FFF5FBFF7FC printf ("%p\n", &m1[4]);//0x7fff5fbff800//access, from high to low access printf ("-------------------------\ n"); int x = 5; int y = ten; printf ("%p\n", &x);//0x7fff5fbff7b8 printf ("%p\n", &y);//0x7fff5fbff7b4, can be seen from the high end of the allocation of space int S1 =-5;/ / 0000 0000 0000 0000 0000 0000 0101 In accordance with the complement store, the absolute value is reversed plus one printf ("%d\n", S1);//1111 1111 1111 1111 1111 1111 1111 1011 FF FF FF FB//Breakpoint </span>
"Learning the path to iOS: C-language" binary. bit operations. Stack