1. 程式人生 > >C++ 設定某一位的值

C++ 設定某一位的值

#include <iomanip>
#include <iostream>
void bithandle(unsigned char value, unsigned char bit)
{
	using namespace std;

	unsigned int val0 = 0xFFFFFFFF;

	val0 &= (0xFFFFFFFF  ^ ((unsigned int)1 << bit));
	val0 |= ((unsigned int)value << bit);

	cout << "value = %x" << hex << val0 << endl;
}


int main(int argc, _TCHAR* argv[])
{
	using namespace std;
	char ch;
	
	bithandle(0, 0);
	bithandle(0, 1);
	bithandle(0, 2);

	system("pause");
	return 0;
}