简单位操作(NVIDIA)

#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;
}


// 运行结果


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值