Requires only one variable to be implemented with a handsome position shown:
Solution One: Universal thinking
#define HALF_BITS_LENGTH 4
This value is half the length of the memory storage unit, which is 4bit in this question.
#define FULLMASK 255
This number represents the mask of a whole bit, in binary notation, it is 11111111.
#define LMASK (Fullmask << half_bits_length)
This macro represents the mask of left bits, in binary notation, which is 11110000.
#define RMASK (Fullmask >> half_bits_length)
This number represents the mask of right bits, in binary notation, which represents 00001111.
#define RSET (b, n) (b = (Lmask & B) ^ n))
This macro, set the right side of B to n
#define LSET (b, n) (b = (Rmask & B) ^ (n<< half_bits_length)))
This macro, set the left of B to n
#define Rget (b) (Rmask & B)
This macro gets the value on the right side of B.
#define Lget (b) ((Lmask & B) >>half_bits_length)
This macro gets the value of the left side of B.
#define GRIDW 3
This number represents the line width of the generals move range.
#include <stdio.h>
#define HALF_BITS_LENGTH 4
#define FULLMASK 255
#define LMASK (Fullmask << half_bits_length)
#define RMASK (Fullmask >> half_bits_length)
#define RSET (b, n) (b = (Lmask & B) ^ n))
#define LSET (b, n) (b = (Rmask & B) ^ (n<< half_bits_length)))
#define Rget (b) (Rmask & B)
#define Lget (b) ((Lmask & B) >>half_bits_length)
#define GRIDW 3
int main ()
{
Unsignedchar b;
For (LSET (b, 1); Lget (b) <= GRIDW * GRIDW; LSET (b, (Lget (b) + 1)))
For (RSET (b, 1); Rget (b) <= GRIDW * GRIDW; RSET (b, (Rget (b) + 1)))
if (Lget (b)% GRIDW!= rget (b)% GRIDW)
printf ("A =%d, B =%d\n", Lget (b), Rget (b));
Return0;
Solution Two: Ingenious thinking
struct {
Unsignedchar A:4;
Unsignedchar B:4;
I
for (I.A = 1; i.a <= 9; i.a++)
for (i.b= 1; i.b <= 9; i.b++)
if (i.a% 3!= i.b% 3)
printf ("A =%d, B =%d\n", I.A,I.B);