[C#]我自己写的一个对字节中每位进行修改值的函数

2022-05-07 18:27:06 浏览数 (1)

位操作方法

代码如下:

1. 设置字节中某位的值

代码语言:javascript复制
static public Byte s_SetBit(Byte byTargetByte, int nTargetPos, int nValue)
{
	int nValueOfTargetPos = -1;

	if (nValue != 0 && nValue != 1)
	{
		return 0;
	}
	if (!(nTargetPos >= 0 && nTargetPos < 8))
	{
		return 0;
	}

	switch (nTargetPos)
	{
		case 0:
			nValueOfTargetPos = (byTargetByte >> 7) & 0x01;
			if (nValue == 1)
			{
				byTargetByte = Convert.ToByte(byTargetByte | 0x80);
			}
			else if (nValue == 0)
			{
				byTargetByte = Convert.ToByte(byTargetByte & 0x7f);
			}
			break;

		case 1:
			nValueOfTargetPos = (byTargetByte >> 6) & 0x01;

			if (nValue == 1)
			{
				byTargetByte = Convert.ToByte(byTargetByte | 0x40);
			}
			else if (nValue == 0)
			{
				byTargetByte = Convert.ToByte(byTargetByte & 0xbf);
			}
			break;

		case 2:
			nValueOfTargetPos = (byTargetByte >> 5) & 0x01;

			if (nValue == 1)
			{
				byTargetByte = Convert.ToByte(byTargetByte | 0x20);
			}
			else if (nValue == 0)
			{
				byTargetByte = Convert.ToByte(byTargetByte & 0xdf);
			}
			break;

		case 3:
			nValueOfTargetPos = (byTargetByte >> 4) & 0x01;

			if (nValue == 1)
			{
				byTargetByte = Convert.ToByte(byTargetByte | 0x10);
			}
			else if (nValue == 0)
			{
				byTargetByte = Convert.ToByte(byTargetByte & 0xef);
			}
			break;

		case 4:
			nValueOfTargetPos = (byTargetByte >> 3) & 0x01;

			if (nValue == 1)
			{
				byTargetByte = Convert.ToByte(byTargetByte | 0x08);
			}
			else if (nValue == 0)
			{
				byTargetByte = Convert.ToByte(byTargetByte & 0xf7);
			}
			break;

		case 5:
			nValueOfTargetPos = (byTargetByte >> 2) & 0x01;

			if (nValue == 1)
			{
				byTargetByte = Convert.ToByte(byTargetByte | 0x04);
			}
			else if (nValue == 0)
			{
				byTargetByte = Convert.ToByte(byTargetByte & 0xfb);
			}
			break;

		case 6:
			nValueOfTargetPos = (byTargetByte >> 1) & 0x01;

			if (nValue == 1)
			{
				byTargetByte = Convert.ToByte(byTargetByte | 0x02);
			}
			else if (nValue == 0)
			{
				byTargetByte = Convert.ToByte(byTargetByte & 0xfd);
			}
			break;

		case 7:
			nValueOfTargetPos = byTargetByte & 0x01;

			if (nValue == 1)
			{
				byTargetByte = Convert.ToByte(byTargetByte | 0x01);
			}
			else if (nValue == 0)
			{
				byTargetByte = Convert.ToByte(byTargetByte & 0xfe);
			}
			break;

		default:
			break;
	}

	if (nValueOfTargetPos != -1)
	{
		return byTargetByte;
	}
	else
	{
		return 0;
	}
}

测试案例:

把每位全为1的字节置0

代码语言:javascript复制
Byte b = Convert.ToByte(0xff);
richTextBoxMain.Text = "byte初始状态: "   Convert.ToString(b, 2).PadLeft(8, '0')   "rn";
for (int i = 0; i < 8; i  )
{
	b = s_SetBit(b, i, 0);
	richTextBoxMain.Text  = "byte修改第"   i   "位后的结果: ";
	richTextBoxMain.Text  = Convert.ToString(b, 2).PadLeft(8, '0')   "rn";
}

结果如下:

byte初始状态: 11111111 byte修改第0位后的结果: 01111111 byte修改第1位后的结果: 00111111 byte修改第2位后的结果: 00011111 byte修改第3位后的结果: 00001111 byte修改第4位后的结果: 00000111 byte修改第5位后的结果: 00000011 byte修改第6位后的结果: 00000001 byte修改第7位后的结果: 00000000

2. 获得字节中某位的值

代码语言:javascript复制
static public int s_GetBit(Byte byTargetByte, int nTargetPos)
{
	int nValue = -1;

	switch (nTargetPos)
	{
		case 0:
			nValue = (byTargetByte >> 7) & 0x01;
			break;
		case 1:
			nValue = (byTargetByte >> 6) & 0x01;
			break;
		case 2:
			nValue = (byTargetByte >> 5) & 0x01;
			break;
		case 3:
			nValue = (byTargetByte >> 4) & 0x01;
			break;
		case 4:
			nValue = (byTargetByte >> 3) & 0x01;
			break;
		case 5:
			nValue = (byTargetByte >> 2) & 0x01;
			break;
		case 6:
			nValue = (byTargetByte >> 1) & 0x01;
			break;
		case 7:
			nValue = byTargetByte & 0x01;
			break;
		default:
			break;
	}

	return nValue;
}

0 人点赞