Problem description
Assume that only two pieces of chess are left in Chinese chess. Chinese people all know the basic rules: the handsome guy can only move up or down the left and right sides, but cannot move in oblique directions. In this case, all possible positions will be handsome. Only one byte storage variable is required in the code.
Answer questions
If we discard the condition that only one byte is used to store variables, this question is at the helloworld level. The question is how to put two variables in two for loops in one byte, in the initial analysis, the author used a large number of bitwise operations to store two variables in the same byte, and the program became "bloated. For specific code, see:
1 #include<stdio.h> 2 #define HALF_BITS_LENGTH 4 3 #define FULLMASK 255 4 #define LMASK (FULLMASK << HALF_BITS_LENGTH) 5 #define RMASK (FULLMASK >> HALF_BITS_LENGTH) 6 #define RSET(b,n) (b = (b & LMASK) | (n)) 7 #define LSET(b,n) (b = ((b & RMASK) | ((n) << HALF_BITS_LENGTH))) 8 #define RGET(b) (b & RMASK) 9 #define LGET(b) ((b & LMASK)>>HALF_BITS_LENGTH)10 #define GRIDW 311 12 int main()13 {14 unsigned char b;15 for(LSET(b,1);LGET(b) <= GRIDW * GRIDW;LSET(b,(LGET(b)+1)))16 {17 for(RSET(b,1);RGET(b) <= GRIDW * GRIDW;RSET(b,(RGET(b))+1))18 {19 if(LGET(b) % GRIDW != RGET(b) % GRIDW)20 {21 printf("A=%d,B=%d\n",LGET(b),RGET(b));22 }23 }24 }25 return 0;26 }
However, the above method is too cumbersome. At the end of the question, we provide a simple method to implement the question using the "bit" field.
The explanation of bit domains in Baidu encyclopedia is: "bit domains mean that when information is stored, they do not need to occupy a complete byte, but only need to occupy a few or one binary bit. For example, when storing a switch value, there are only two States: 0 and 1. Use one binary digit. To save storage space and simplify processing, the C language also provides a data structure called "bit domain" or "bit segment ". The so-called "bit field" refers to dividing the binary character in a byte into several different regions and showing the digits of each region. Each domain has a domain name, which allows operations by domain name in the program. In this way, several different objects can be represented by a byte binary field ."
Bit fields are defined in a way similar to struct:
Struct bit domain structure name {bit domain list };
The format of the bit domain list is: type description Character Domain Name: Bit domain length. For example, struct bs {int a: 8; int B: 2; int c: 6 ;}; with the bit field, we can directly specify the first four and last four digits of a single position as two sub-members of a single structure, which is equivalent to the ability to define two variables. The specific code is as follows:
1 #include<stdio.h> 2 struct { 3 unsigned char a:4; 4 unsigned char b:4; 5 } i; 6 int main() 7 { 8 for(i.a = 1; i.a <= 9;i.a++) 9 for(i.b = 1;i.b <=9;i.b++)10 if(i.a % 3 != i.b % 3)11 printf("A=%d,B=%d\n",i.a,i.b);12 return 0;13 }
References:
1. The beauty of programming P13-P15
2. Baidu Encyclopedia: Bit domain http://baike.baidu.com/view/1256879.htm