您的位置:首页 > 其它

简单位操作(NVIDIA)

2012-09-24 14:56 141 查看
#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 Paper
SH-01-A-Arch&Content

4. 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 bits
for(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;
}


// 运行结果

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: