题意:请写出一个程序,输出将、帅所有合法的位置,要求在代码中只使用一个变量
解法一:位运算,9个位置状态可以用4bit位表示,一共需要1byte。左边4bit,代表A的位置,右边4bit,代表B的位置,然后每次都去更新bit位即可。
package com.leetcode.test;
public class Test {
public static void main(String[] args) {
for (int i = 0x10; i < 0x90; i += 0x10){
for (i= (i & 0xF0) | (i >> 4); (++i & 0xF) < 10; ) {
if (((i & 0xF) - (i >> 4)) != 3 && ((i & 0xF) - (i >> 4)) != 6)
{
System.out.println(String.format("A= %d , B =%d" , (i>>4),(i&0xF)));
System.out.println(String.format("A= %d , B =%d" , (i&0xF),(i>>4)));
}
}
}
}
}



