当前位置:网站首页>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;
}
边栏推荐
- 1. Editing weapon VIM
- btrace-(字节码)动态跟踪工具
- Fundamentals of software testing
- Implement a server with multi process concurrency
- There is no solution to the decryption error of the remote user 'sa' and the service master password mapped from the remote server 'to the local user' (null) /sa '
- taobao.trade.memo.add( 对一笔交易添加备注 )接口,淘宝店铺插旗接口,淘宝订单插旗API接口,oAuth2.0接口
- Fabric. Keep the original level when JS element is selected
- Onnx+tensorrt: write preprocessing operations to onnx and complete TRT deployment
- ##51单片机实验之简易验证码发生器
- 使用mathtype编辑公式,复制粘贴时设置成仅包含mathjax语法的公式
猜你喜欢
jmeter脚本参数化
[email protected] : The platform “win32“ is incompatible with this module."/>
info [email protected] : The platform “win32“ is incompatible with this module.
LeetCode 209. Minimum length subarray
MFC 定时器使用
buuctf-pwn write-ups (7)
LeetCode 209. 长度最小的子数组
复用和分用
About text selection in web pages and counting the length of selected text
【apipost】使用教程
Actual combat sharing of shutter screen acquisition
随机推荐
华为面试题: 没有回文串
info [email protected] : The platform “win32“ is incompatible with this module.
Fabric. JS manual bold text iText
Simple verification code generator for 51 single chip microcomputer experiment
There is no solution to the decryption error of the remote user 'sa' and the service master password mapped from the remote server 'to the local user' (null) /sa '
Li Chuang EDA learning notes 15: draw border or import border (DXF file)
YoloV6训练:训练自己数据集遇到的各种问题
数据库连接池和数据源
Fundamentals of software testing
Huawei interview question: no palindrome string
Fabric.js 自由绘制椭圆
obsidian安装第三方插件——无法加载插件
taobao.logistics.dummy.send( 无需物流发货处理 )接口,淘宝店铺发货API接口,淘宝订单发货接口,淘宝r2接口,淘宝oAu2.0接口
871. 最低加油次数 : 简单优先队列(堆)贪心题
Chapter 9: xshell free version installation
Ad20 cannot select the solution of component packaging in PCB editor
2、const 型指针
The use of TestNG, the testing framework (II): the use of TestNG XML
LeetCode 2320. Count the number of ways to place the house
Makefile separates file names and suffixes