In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "what are the operation types in C language". The editor shows you the operation process through an actual case, and the operation method is simple, fast and practical. I hope this article "what are the operation types in C language" can help you solve the problem.
I. Overview
The following four types of operations are supported in C language
Operation type operator four operations +, -, *, /,% relational operation, =, =,! = logical operation &, | |! Bit operation &, |, ^, > >, relational operation > assignment operation)
Four operations
Plus or minus sign > multiplication and division > addition and subtraction
Relational operation
Size comparison operation > equality comparison operation
Assignment operation
Let's take a look at a piece of code to get an in-depth sense of the priority of operation:
# include int main () {int a = 1; int b = 2; int c = 0; c = a! = b + a * b; printf ("c =% d\ n", c); return 0;}
The following is the output result, you can see that the output result of c is 1, why 1? This is because the multiplication priority is greater than the addition priority than the relational operation priority, so the program execution process is like this: first calculate a * b, then add the result of a * b to b to get 4, and finally compare a! = 4, the result is of course 1, and we can also observe the execution of the code through disassembly.
C = a! = b + a * b assembly code
So if you want to get a! = b plus a * b, be sure to add parentheses as follows:
# include int main () {int a = 1; int b = 2; int c = 0; c = (a! = b) + (a * b); printf ("c =% d\ n", c); return 0;}
This is the only way to get the correct results, as shown below:
Summary:
There are two kinds of integer division: quotient (/) and remainder (%).
Floating point division is the same as division in mathematics, and the result is floating point.
The priority of different operations is different. You can use parentheses to change the priority of operation.
Different types of operations should be avoided in the same expression (four operations, relational operations)
Logical operation and bit operation
Logical operation (& &, llthol!)
The participant of a logical operation is a logical value (true or false)
Any non-zero value is true in logical operations
Any zero value is false in logical operation
& & Operation left Operand right Operand result 000010100111 | Operation left Operand right Operand result 000011101111
Short-circuit rule in logic operation
For & & operation
From left to right, if one Operand is false, the whole expression is false
Other operands after the first false Operand are no longer calculated
For | | Operation
From left to right, if one Operand is true, the whole expression is true
Other operands after the first true Operand are no longer calculated
Take non-operation (!)
Monocular operation (only one Operand is required), and the result is a logical value
The result of choosing from the true value is false.
Take the false value and get the result to be true
Don't say much, go to the code:
# include int main () {int a = 1; int b = 2; int c = 0; c = a & & b; printf ("c =% d\ n", c); c =! (a-b) | | (c
< b); printf("c = %d\n", c); c = 10000; c = !!c; printf("c = %d\n", c); return 0;} 下面为运行结果: 位运算(&,|,~,^,) 直接对数据的二进制位进行操作 位运算的基本单位是二进制位,所以也是一种0和1的操作 可以使用括号()改变位运算的运算顺序 位运算的操作数只能是整型数(浮点数不能直接进行位运算) 运算符含义示例优先级~按位求反~0101 ->10101 (high) right shift: low position shift out, high position complement bit 0101 > > 2-> 00012 & bitwise and 0111 & 1100-> 01003 ^ bitwise XOR: same is 0, different is 10111 ^ 1100-> 10114 | bitwise or 0111 | 1100-> 11115 (low)
Note:
1. The calculation method of bitwise and logical sum is the same: both are 1, the result is 1, otherwise it is 0
two。 Bitwise OR is the same as logical OR: both are 0, the result is 0, otherwise it is 1
Bit operation example
Change the second binary position 1 of the integer 5
Invert the fourth binary bit of the integer 7
Invert the last two digits of integer 2
Move the integer 15 2 bits to the right, and then move the second binary position 0
Let the binary number of the variable a be 10101101. What is the value of b if you want to invert the middle 4 bits of an and keep the rest unchanged by the operation a ^ b?
The code is as follows:
# include int main () {printf ("c =% d\ n", 5 | 2); printf ("c =% d\ n", 7 ^ 8); printf ("c =% d\ n", 2 ^ 3); printf ("c =% d\ n", (15 > > 2) & 13); printf ("c =% d\ n", 173 ^ 60); return 0;}
The running results are as follows:
Special attention should be paid here: XOR (^) can be used for one or several inverts, which is commonly used in projects!
Operation priority (priority from top to bottom is high to low)
Plus or minus sign > logical non > reverse by bit
Multiply and divide > add and subtract > move left and right by bit
Size comparison operation > equality comparison operation
Bitwise and > bitwise XOR > bitwise OR
Logic and > logic or
Assignment operation
Summary
There is a special short circuit rule in the logic operation, and the result will not be calculated downwards after the result is determined.
In C language, the true value corresponds to the non-zero value and the false value corresponds to the zero value.
Bit operation directly operates on the binary bits of data.
The operands of bit operations can only be integers (floating point numbers cannot perform bit operations directly)
5. in-depth parsing bit operation
The nature of different types is:
The amount of memory occupied is different, such as 2 bytes for short and 4 bytes for int.
Specific data are represented in different ways
Positive integers are represented by source codes, and negative integers are represented by complement codes.
The binary representation of integer type and floating point type is different.
What you need to know clearly when calculating bits.
Type of Operand (amount of memory occupied)
Is the Operand positive or negative (symbolic bits, data representation)
Different types of operands are automatically aligned before bit operations (complementary symbol bits)
Such as the following code:
Short a = 1% int b = 4 * * int c = a | b * tf * * ("c =% d\ n", c)
B is the int type, accounting for 4 bytes, an is the short type, accounting for 2 bytes. So a has to fill in the sign bit first, and because an is a positive number, it is 0, so that the bit operation can be carried out, and the result of c is 5.
Let's look at a piece of code:
# include int main () {short a = 1; short b = 2; int c = a-b; c = c > 4; printf ("c =% d\ n", c); c = c *-1 * 16 > > 4; printf ("c =% d\ n", c); printf ("c =% d\ n", 16)
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.