简单位操作(NVIDIA)

来源:互联网 发布:求稀疏矩阵的行列和 编辑:程序博客网 时间:2024/06/08 03:52

#include "stdafx.h"#include <stdio.h>#include <iostream>#include <stdlib.h>#include <math.h>using namespace std;/*********************************************************************NVIDIA 2013 Software Engineering Campus Recruitment Exam Test PaperSH-01-A-Arch&Content4. Please implement a funciton to only modify partial bits of a 32-bit unsigned integer variable.The prototype of the function is as follows:void modify(uint_32 &var, uint_8 bit_high, unit_8 bit_low, uint_32 value);Example:unit_32 register = 0xbed;modify(register,7,4,0xa);// register = 0xbad**********************************************************************/typedef unsigned int uint_32; typedef unsigned int uint_8;void modify(uint_32 &var, uint_8 bit_high, uint_8 bit_low, uint_32 value){int i = 0;int j =0;// clear or reset the corresponding bits for(i = bit_low;i<=bit_high;i++){var &=~(0x01<<i);}// set the corresponding bitsfor(i = bit_low;i<=bit_high;i++){j = value & 0x01;var |=((value&0x01)<<i);value =value>> 1;}}int _tmain(int argc, _TCHAR* argv[]){uint_32 register0 = 0xbed;modify(register0, 3, 0, 0xa);printf("the Modify result is %x\n",register0);return 0;}


// 运行结果