当前位置:网站首页>Arithmetic operations and related exercises in C language
Arithmetic operations and related exercises in C language
2022-07-02 14:56:00 【theskylife】
Write it at the front :
Reading this article can solve the following problems : How to use it C Language performs arithmetic operations ? How to use bit operation to speed up your code ? How to skillfully use bit operation in actual combat code ? Multiple ways to realize the same function .
1. Operator
Operator | explain | Example |
---|---|---|
= | Assignment operator | a = b; |
+ 、- 、* 、/、() | Basic four principles | a = (b + c) * d; |
% | The remainder operator | a = b % 2; |
&、 |、 ^、 ~ | An operation ( A very important class ) | a = ~b | c; |
<<、>> | Move left and right | a = b >>2; |
1.1 An operation
Bit operations are done in binary .
1.1.1 Bitwise AND (&)
All binary bits are 1 The result is 1, There is one for 0, Then for 0. When the number of digits is insufficient , Front complement 0.
Such as :
- 2&3 --> 10 & 11 --> 10, Convert decimal to zero 2
- 8 & 3 --> 1000 & 11 --> 1000 & 0011–>0, Convert decimal to zero 0
Use scenarios :
Judge the numbers n Can you divide 2,n & 1 by 1 You can't divide , by 0 Then you can divide
1.1.2 Press bit or (|)
One of the binary bits is 1, Then for 1, All for 0 Then for 0. When the number of digits is insufficient , Front complement 0.
Such as :
- 2|3 --> 10 | 11 --> 11, Convert decimal to zero 3
- 8 | 3 --> 1000 | 11 --> 1000 | 0011 -->1011, Convert decimal to zero 11
1.1.3 bitwise exclusive or operator (^)
The same binary bit is 0, Different for 1. When the number of digits is insufficient , Front complement 0.
Such as :
- 2^3 --> 10 ^ 11 --> 01, Convert decimal to zero 1
- 8 ^ 3 --> 1000 ^ 11 --> 1000 ^ 0011–>1011, Convert decimal to zero 11.
Use scenarios :
- ^ yes ^ The inverse operation of .
- a ^ b = c, be c ^ b = a and c ^ a = b.
- a ^ a = 0.
- 0 ^ a = a.
1.1.4 According to the not (~)
Binary digit 0 Turn into 1,1 Turn into 0
Such as :
- ~3 --> ~11 -->11(30 individual 1)00[ Complement code ], Convert decimal to zero -4
Complement code = ~ Original code + 1
When the original code is reversed , The sign bits remain the same
give an example :
1 The original code of :0…0(31 individual 0)1, ~ Original code : 01…1(30 individual 1)0, Complement code :01…1(31 individual 1)
-1 The original code of : 10…0(30 individual 0)1, ~ Original code : 1…1(31 individual 1)0, Complement code :1…1(32 individual 1)
-1 Complement =1 All original codes are reversed +1
1.2 Move left and right
1.2.1 Move left
Move left : Low complement 0
Move left 1 position , It's equivalent to doubling .
Such as : 2 << 1, Convert decimal to zero 4.
1.2.2 Move right
Move right : High complement sign bit
Such as :3 >> 1, Convert decimal to zero 1.
2. Data type conversion
There are two types of data conversion : Strong conversion and weak conversion
Strong transformation : Use int,double And so on , Will lose precision .
Weak transformation : Use similar 3*1.0/2 Method , take int Type to double.
3. Hexadecimal conversion
3.1 Binary to decimal
10010=12**4+12**1=18
3.2 Hexadecimal to decimal
8F=816**1+1516**0=143
3.3 Decimal to binary
Method 1: Use short division , Until 0, Then write from bottom to top ;
Method 2: Make up
27=16+8+2+1=24+23+21+20=11010
4.math Common functions
Write it at the front :
Use the following functions , Import required math library ;
At compile time , Use the command
gcc test.c -lm
4.1 pow function : Exponential function
Prototype : double pow(double a, double b);
Example :pow(4,2)=16.0000
4.2 sqrt function : Square root function
Prototype : double sqrt(double X);
Example :pow(16)=4.0000
4.3 ceil function : Round up the function
Prototype : double ceil(double X);
Example :ceil(2.79)=3.0000
4.4 floor function : Round down the function
Prototype : double floor(double X);
Example :floor(2.79)=2.0000
4.5 abs function : Integer absolute value function
Prototype :int abs(int X);
Example :abs(-2)=2
4.6 fabs function : Real absolute value function
Prototype :double fabs(double X);
Example :fabs(-2.6)=2.6
4.7 log function : With e Is the base logarithm function
Prototype :double log(double X);
Example :log(4)=1.386294
4.8 log10 function : With e Is the base logarithm function
Prototype :double log10(double X);
Example :log10(4)= 0.602060
4.9 acos function :arccos function
Prototype :double acos(double X);
X Is the radian value of the angle
Example :acos(-1)=3.1415926
5. Fixed width integer type
#include <stdio.h>
#include <inttypes.h>
int main(){
printf("%zu\n", sizeof(int64_t)); // Check the memory size
printf("%s\n", PRId64); // Format macro constants
printf("INT32_MIN : %" PRId32 ", INT32_MAX : %" PRId32 "\n", INT32_MIN, INT32_MAX); //32 Bit shaping min max
printf("INT64_MIN : %" PRId64 ", INT64_MAX : %" PRId64 "\n", INT64_MIN, INT64_MAX); //64 Bit shaping min max
int64_t n = 7;
printf("%+"PRId64"\n", n);
return 0;
}
6. Exercises
subject 1
Enter a number a, Output its cube root . Click to see the answer
subject 2:
seek π Value . Click to see the answer
subject 3:
Enter an angle value , And convert it to radian value . Click to see the answer
subject 4:
Loop in two numbers a,b, And exchange two values . Click to see the answer
Refer to the answer
subject 1 answer
// subject 1
#include<stdio.h>
#include<math.h>
int main(){
double a;
scanf("%lf", &a);
// solution 1, Understand thoughts
printf("%lf\n", pow(a, 1.0 / 3));
// solution 2
printf("%lf\n", cbrt(a));
return 0;
}
subject 2 answer
#include<stdio.h>
#include<math.h>
#define pi acos(-1)
// #define pi 3.14
int main(){
double a;
scanf("%lf", &a);
printf("%lf\n", a * pi / 180);
return 0;
}
subject 3 answer
#include<stdio.h>
#include<math.h>
#define pi acos(-1)
// #define pi 3.14
int main(){
double a;
scanf("%lf", &a);
printf("%lf\n", a * pi / 180);
return 0;
}
subject 4 answer
// Method 1
#include<stdio.h>
int main(){
int a, b;
while (~scanf("%d%d", &a, &b)){
printf("a is %d, b is %d\n", a, b);
int c;
c = a;
a = b;
b = c;
printf("after change: a is %d, b is %d\n", a, b);
}
return 0;
}
// Method 2
#include<stdio.h>
int main(){
int a, b;
while (~scanf("%d%d", &a, &b)){
printf("a is %d, b is %d\n", a, b);
a = a + b;
b = a - b;
a = a - b;
printf("after change: a is %d, b is %d\n", a, b);
}
return 0;
}
// Method 3
#include<stdio.h>
int main(){
int a, b;
while (~scanf("%d%d", &a, &b)){
printf("a is %d, b is %d\n", a, b);
b ^= a;
a ^= b;
printf("after change: a is %d, b is %d\n", a, b);
}
return 0;
}
边栏推荐
- STM32 standard firmware library function name memory (II)
- [untitled] leetcode 2321 Maximum score of concatenated array
- 一张图彻底掌握prototype、__proto__、constructor之前的关系(JS原型、原型链)
- Implement a server with multi process concurrency
- 途家木鸟美团夏日折扣对垒,门槛低就一定香吗?
- Onnx+tensorrt: write preprocessing operations to onnx and complete TRT deployment
- 天猫商品详情接口(APP,H5端)
- MQ tutorial | exchange (switch)
- 广州市应急管理局发布7月高温高湿化工安全提醒
- Large top heap, small top heap and heap sequencing
猜你喜欢
fatal: unsafe repository is owned by someone else 的解决方法
MathML to latex
Thoroughly master prototype__ proto__、 Relationship before constructor (JS prototype, prototype chain)
Uniapp automated test learning
Fabric. JS free draw circle
forEach的错误用法,你都学废了吗
C#代码审计实战+前置知识
STM32-DAC实验&高频DAC输出测试
Li Chuang EDA learning notes 15: draw border or import border (DXF file)
Obsidian installs third-party plug-ins - unable to load plug-ins
随机推荐
btrace-(字节码)动态跟踪工具
Fabric. JS upper dash, middle dash (strikethrough), underline
Li Chuang EDA learning notes 15: draw border or import border (DXF file)
C code audit practice + pre knowledge
STM32标准固件库函数名记忆(二)
报错:npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
LeetCode_字符串_简单_412.Fizz Buzz
threejs的控制器 立方體空間 基本控制器+慣性控制+飛行控制
obsidian安装第三方插件——无法加载插件
STM32标准固件库函数名(一)
taobao.logistics.dummy.send( 无需物流发货处理 )接口,淘宝店铺发货API接口,淘宝订单发货接口,淘宝r2接口,淘宝oAu2.0接口
Tujia muniao meituan has a discount match in summer. Will it be fragrant if the threshold is low?
Fatal: unsafe repository is owned by someone else
Huawei interview question: no palindrome string
为什么只会编程的程序员无法成为优秀的开发者?
Implement a server with multi process concurrency
LeetCode 2320. 统计放置房子的方式数
蜻蜓低代码安全工具平台开发之路
3. Function pointers and pointer functions
MQ教程 | Exchange(交换机)