当前位置:网站首页>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;
}
边栏推荐
- PTA题库 ===>复数四则运算,一帮一,考试座位号(7-73)
- 871. 最低加油次数 : 简单优先队列(堆)贪心题
- qml 弹窗框架,可定制
- Advanced C language (realize simple address book)
- What is erdma? Popular science cartoon illustration
- fatal: unsafe repository is owned by someone else 的解决方法
- Contrôleur pour threejs cube Space Basic Controller + Inertial Control + Flight Control
- Fabric.js 橡皮擦的用法(包含恢复功能)
- Chapter 9: xshell free version installation
- 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 '
猜你喜欢
LeetCode 2320. 统计放置房子的方式数
STM32-DAC实验&高频DAC输出测试
【空间&单细胞组学】第1期:单细胞结合空间转录组研究PDAC肿瘤微环境
It's no exaggeration to say that this is the most user-friendly basic tutorial of pytest I've ever seen
关于网页中的文本选择以及统计选中文本长度
Thoroughly master prototype__ proto__、 Relationship before constructor (JS prototype, prototype chain)
Fabric. JS upper dash, middle dash (strikethrough), underline
buuctf-pwn write-ups (7)
[email protected]: The platform “win32“ is incompatible with this module."/>
info [email protected]: The platform “win32“ is incompatible with this module.
Error: NPM warn config global ` --global`, `--local` are deprecated Use `--location=global` instead.
随机推荐
LeetCode 2310. 个位数字为 K 的整数之和
NLA natural language analysis realizes zero threshold of data analysis
taobao. logistics. dummy. Send (no logistics delivery processing) interface, Taobao store delivery API interface, Taobao order delivery interface, Taobao R2 interface, Taobao oau2.0 interface
String matching problem
Xilinx Vivado set *. svh as SystemVerilog Header
Fabric.js 上划线、中划线(删除线)、下划线
jmeter脚本参数化
MQ tutorial | exchange (switch)
C# richTextBox控制显示最大行数
Fabric. JS dynamically set font size
C#代码审计实战+前置知识
Error: NPM warn config global ` --global`, `--local` are deprecated Use `--location=global` instead.
Xilinx Vivado set *.svh as SystemVerilog Header
华为面试题: 没有回文串
##51单片机实验之简易验证码发生器
数据库连接池和数据源
STM32标准固件库函数名(一)
Wechat applet uses towxml to display formula
Obsidian installs third-party plug-ins - unable to load plug-ins
Some Chinese character codes in the user privacy agreement are not standardized, which leads to the display of garbled codes on the web page. It needs to be found and handled uniformly