📄 bitoperate.cpp
字号:
// BitOperate.cpp: implementation of the BitOperate class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "BitOperate.h"
#include "iostream.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
BitOperate::BitOperate()
{
}
BitOperate::~BitOperate()
{
}
/*
功能:把ch从右数的第position位设置为value;
*/
byte BitOperate::bitSet(byte ch, short position, bool value)
{
byte temp = ch;
if(position>8||position<1)
{
cout<<"Out of Bound! Position must be a number between 1 --- 8 "<<endl;
return ch;
}
bool bit = bitAt(temp,position);
if(bit^value)//如果不相同的话
{
if(bit == 0)
{
byte m = (byte)value;
for(int i = 1;i<position;i++)
m = m<<1;
ch+=m;
return ch;
}
else
{
byte m = (byte)bit;
for(int i = 1;i<position;i++)
m = m<<1;
ch-=m;
return ch;
}
}
else return ch;
}
/*
功能:返回ch从右数第i位的值;
*/
bool BitOperate::bitAt(byte ch, short position)
{
byte temp = ch;
if(position>8||position<1)
{
cout<<"Out of Bound! Position must be a number between 1 --- 8 "<<endl;
return false;
}
for(int j = 1;j<position;j++)
temp = temp>>1;
if(temp%2==0)return 0;
else return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -