您的位置:首页 > 其它

关于有符号数的一些测试

2015-03-31 13:05 197 查看
#include <stdio.h>
#include <stdint.h>

#define opposite(x) (~(x) + 1)
#define lowBit(x) ((x) & -(x))
/* 有符号数溢出的测试 */
void test0() {
int8_t n = 0;
do {
printf("%hhd\n", n);//[0,127]->[-128,-1]
n++;
} while (n);
}
/* 求有符号数的相反数 */
void test1() {
int8_t n = 67, n1 = -53;
printf("%hhd\n", opposite(n));  //-67
printf("%hhd\n", opposite(n1)); //53
}
/* 逻辑右移与算术右移的测试 */
void test2() {
uint8_t n = 0x90;
uint8_t tmp1, tmp2;
tmp1 = n >> 1;                  //逻辑右移
tmp2 = (int8_t)n >> 1;          //算术右移
printf("tmp1 = %hhX\n", tmp1);  //0x48
printf("tmp2 = %hhX\n", tmp2);  //0xC8
}

void test3() {
uint8_t n = 80;
printf("%hhu\n", lowBit(n));
}

int main() {
//test0();
//test1();
test2();
//test3();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: