8*8的棋盘的二进制表示法

#include<stdio.h>
#include<math.h>
/*8*8的棋盘,用8未表示,前四位表示列,后四位表示行*/
unsigned char binary_row = 0x07; /*求与后只有行*/
unsigned char binary_col = 0x70; /*求与后只有列*/

bool isSameRow(unsigned char a, unsigned char b)
{
	a = a & binary_row;
	b = b & binary_row;
	if( a == b)
		return true;
	return false;
}

bool isSameCol(unsigned char a, unsigned char b)
{
	a = a & binary_col;
	b = b & binary_col;
	if(a == b)
		return true;
	return false;
}

bool IsDiagonal(unsigned char a, unsigned char b)
{
	/*右移a b求列的差  行的差可直接求与*/
	if(abs( (a>>4) - (b>>4) ) == abs( (a&binary_row) - (b&binary_row) ))
		return true;
	return false;
}

void test()
{
	unsigned char apos = 0x40;
	unsigned char bpos = 0x11;
	unsigned char cpos = 0x22;
	unsigned char dpos = 0x14;
	unsigned char epos = 0x54;

	if(isSameCol(bpos, dpos))
		printf("b and d in the same col\n");
	if(isSameRow(dpos, epos))
		printf("d and e in the same row\n");
	if(IsDiagonal(apos, cpos))
		printf("a and c on the diagonal\n");
}

int main()
{
	test();
	return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值