您的位置:首页 > 其它

获取某值的具体某位

2015-09-01 16:47 429 查看
获取某值的具体某位

函数:unsigned char get_bit(unsigned char temp, int bit)

获取某值的具体某位。

参数:temp为传入的值,bit是要获取temp的值具体某一位的值

返回值:函数返回 传入参数temp的第bit位的值。

#include <stdio.h>
#include <stdlib.h>

/************************************************************************/
/*函数:unsigned char get_bit(unsigned char temp, int bit)               */
/*	   获取某值的具体某位。              									*/
/*参数:temp为传入的值,bit是要获取temp的值具体某一位的值						*/
/*返回值:函数返回 传入参数temp的第bit位的值。								*/
/************************************************************************/

unsigned char get_bit(unsigned char temp, int bit)
{
if ( (bit < 0) || (bit > 8) )
{
printf("获取的位值不存在\n");
return -1;
}
else
{
return (temp >> bit)&0x01;
}
}

int main(void)
{
unsigned char temp = 0;
int bit = 0;
unsigned char getbit = 0;

scanf("%d %d", &temp, &bit);
getbit = get_bit(temp, bit);
// 7
// 0000 0111

printf("bit : %x\n", getbit);

system("pause");

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