关于有符号数的一些测试

来源:互联网 发布:大凤号航空母舰知乎 编辑:程序博客网 时间:2024/06/05 06:14
#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;}


0 0