如何从int获得特定位

问题描述 投票:0回答:1

试图模拟WEXITSTATUS功能,这需要获取退出状态返回值的第8位至第15位。

void My_Exit_Status_Check(unsigned int status){
    pid_t pid;
    unsigned  mask, mask2;
    unsigned result[7], result2[8];
    unsigned i;
    unsigned ans1 = 0, ans2=0;
    mask = ((1 << 7) - 1) << 0;
    ans1 = status & mask;
    mask2 =  ((1 << 8) - 1) << 8;
    ans2 = (status >> 0) & mask2;
    if(binary_to_decimal(ans1)==0){
       printf("The exit status for child %d using my status check function is: Normal termination, exit status is: %d should equal %d\n", getpid(), binary_to_decimal(ans2), WEXITSTATUS(status)); 
    }
    else{
       printf("The exit status for child %d using my status check function is: Abnormal termination, signal is: %d\n", getpid(), binary_to_decimal(ans1));
    }
}
c bit-manipulation bitmask
1个回答
0
投票

只需用0xFF00掩盖它,然后向右移动8。假设您从右边和从0开始计数。

© www.soinside.com 2019 - 2024. All rights reserved.