I have a question about the pointer! *z = ~(hap & 0xFF); What does ~ mean in?

Asked 1 weeks ago, Updated 1 weeks ago, 7 views

Hello! I am leaving a question because I have a question while studying C language! First of all, it was understood that the while statement was executed to output the cnt value and the hap value After that, I couldn't understand the if statement, so I left a question.

*z = ~(hap & 0xFF);I understand that hap & 0xFF is 11 in the part I can't find what ~ means. Also, I'm not sure why *z becomes 244.

#include <stdio.h>
unsigned char test(unsigned char*, int, int);

int main(void)
{
    unsigned char message[5] = { 'A', 'B', 'C', 'D' };
    unsigned char ans;
    test(&message[4], 5, 1);
    printf("Result on transmission = %d\n", (int)message[4]);
    ans = test(&message[4], 5, 2);
    printf("Results upon receipt = %d\n", (int)ans);
    return 0;
}

unsigned char test(unsigned char* p, int cnt, int flag)
{
    unsigned short int hap = 0;
    unsigned char* z;
    z = p;
    while (cnt--)
    {
        hap = hap + *p--;
        if (hap & 0xFF00)
        {
            hap = hap & 0xFF;
            hap = hap + 1;
        }
        printf("cnt = %d hap = %d\n", cnt, hap);
    }
    if (flag == 1)
    {
        *z = ~(hap & 0xFF);
        return 0;
    }
    else
        return ~(hap & 0xFF);
}

c

2022-09-19 23:20

1 Answers

~ is a bit inversion operator. Since z is char type, if you think of just 8 bits, the binary value of 11 in ~11 is 00001011 and the bit inversion is 11110100. This value is 244 in decimal.


2022-09-19 23:20

If you have any answers or tips


© 2022 pinfo. All rights reserved.