Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

An example Analysis of bit Operation in C language

Shulou Source: shulou.com Published: 2022-06-01 22:05:59 10月23日 Update

This article mainly explains the "pit example analysis of bit operation in C language". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. let's study and learn the pit example analysis of bit operation in C language.

For example, the following example:

Short a = 0xBC54; / / binary is: 1011 1100 0101 0100short b = 0x07A3; / / binary is: 0000 0111 1010 0011if ((a | b) = = 0xBFF7) {/ / 0xBFF7 binary is: 10111111 1111 0111 puts ("true");} else {puts ("false");}

Will the program print "true" or "false"?

The answer is "false".

Because there is a rule in the bit operation of C language, if the length of the Operand is less than "signed/unsigned int", it will convert the Operand to "signed/unsigned int" and then perform the bit operation. In the above code, when you perform the "a | b" operation, it is actually a "((int) a) | ((int) b)" operation. The type of the expression "a | b" is actually "int", and the value in the above code is 0xFFFFBFF7.

In the above code, if the type of an and b is "unsignedshort", then the program will print "true" because the "a | b" operation is actually a "((unsigned int) a) | (unsigned int) b)" operation.

Thank you for your reading. the above is the content of "the pit example analysis of bit operation in C language". After the study of this article, I believe you have a deeper understanding of the pit example analysis of bit operation in C language. the specific use of the situation also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Language analysis operation binary code learning content actual actually operands programs types examples that is ideas situations articles more knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Information macOS MariaDB NVidia