bitoperate.cpp

来自「C++基于LSB的信息隐藏技术」· C++ 代码 · 共 73 行

CPP
73
字号
// 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 + =
减小字号Ctrl + -
显示快捷键?