In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "Introduction to Bit Operation in C Language". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian to study and learn "Introduction to Bit Operation in C Language" together.
One, digit operator:
1. Position and:
The symbol of bit and is " & ", where many people (including the author himself) learn the logic of the latter and (&&), and then when writing code or reading other people's code, they may confuse the difference between their two forms: bit and symbol is a &, two && are logical AND. The truth table of bits and bits is as follows:
1&0=0 1|1=1 0&0=0 0&1=0
From the truth table, we can see that the characteristic of the bit-OR operation is that only two 0 phases or can get 0, and as long as there is a 1, the result must be 1.
Note--the difference between a bitwise OR and a logical OR: bitwise OR is the logical OR of two operands as a whole.
2, bit or:
The symbol for bit or is "|"And you're going to make the same mistake you just made in writing, and you're going to understand here that a bit or a symbol is a"|"Two."||"It's logical or. Its truth table is as follows:
1|0=1 1|1=1 0|0=0 0|1=1
From the truth table, we can see that the characteristic of bit-OR operation is that only two 0 phases or can get 0, and as long as there is 1, the result must be 1.
Note--the difference between a bitwise OR and a logical OR: bitwise OR is the logical OR of two operands as a whole.
3. Bit inversion:
The symbol for bit inversion is " ~", which is also a form of paying a little attention to logic inversion: bit inversion in C language is " ~ ", logic inversion in C language is " ! "。Bitwise inversion is the bitwise inversion of the binary bits of the operand (1 becomes 0, 0 becomes 1); logical inversion is true (in C, any number other than 0 is true) to false (in C, only 0 means false), false to true. Here's a code walkthrough:
#include
int main(void)
{
unsigned int a = 45;
unsigned int b, c;
b = ~~a; //invert bit by bit, bit by bit operation, 1 becomes 0, 0 becomes 1
c = !! a;//logical inversion, true becomes false, false becomes true
printf("b = %u.\ n", b);
printf("c = %d.\ n", c);
return 0;
}
Output:
b = 45.
c = 1.
From experiments we can discover:
Any number other than zero is logically negated and negated again to get 1.
Any multiple other than zero, bitwise inverted and then inverted, gets itself.
4, position difference or:
Its symbol is ^and its truth table is as follows:
1^1=0 0^0=0 1^0=1 0^1=1
The number of bits exclusive OR characteristics: 2 if the result is equal to 0, unequal result is 1. Memory method: different or is different on or operation up.
II. Summary:
1. Summary of the characteristics of bit and, bit or, bit exclusive or:
Bit and: (any number, in fact, is 1 or 0) and 1 bit and no change, and 0 bit and become 0.
Bit OR: (any number, actually 1 or 0) and 1 bit OR becomes 1, and 0 bit OR does not change.
XORs: (any number, actually 1 or 0) XORs with 1 are reversed, XORs with 0 are unchanged.
2, left shift "" summary:
The shift in C depends on the data type.
For unsigned numbers, a 0 is added to the right when shifted to the left (equivalent to a logical shift).
For unsigned numbers, a 0 is added to the left when shifted to the right (equivalent to a logical shift).
For signed numbers, a 0 is added to the right when shifted to the left (called arithmetic shift, equivalent to logical shift).
For signed numbers, the left side of the sign bit is supplemented when moving right (if positive, complement 0, negative complement 1, called arithmetic shift).
The shifts studied in embedding, and the shifts used, are unsigned numbers.
Thank you for reading, the above is the "C language bit operation introduction" content, after the study of this article, I believe we have a deeper understanding of the C language bit operation introduction this problem, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.