当前位置:网站首页>C knowledge exercise
C knowledge exercise
2022-06-10 19:11:00 【Fate friend I】
List of articles
- Exercise one : Find the number of different bits in two binary numbers
- Exercise 2 : Judge whether the two numbers are the same
- Exercise 3 : Get the odd and even bits in the binary bit
- Exercise 4 :++ Operation level ratio of * high
- Exercise five : Say a word and reverse as required
- Exercise 6 : Long knowledge ,vs Smart
- Knowledge points supplement : Small 、 Big end storage
- Exercise 7 : Digital storage
In front of ++, Add one before returning
After ++, Return first and then add one
Exercise one : Find the number of different bits in two binary numbers
// Find the number of different bits in two binary numbers
#include<iostream>
using namespace std;
int main()
{
int m = 0, n = 0;
cin >> m >> n;
int count = 0;
for (int i = 0; i < 32; i++)
{
if ( ((m >> i) & 1) != ( (n>>i)&i ) )
{
count++;
}
}
printf("%d\n",count);
return 0;
}

Exercise 2 : Judge whether the two numbers are the same
#include<iostream>
using namespace std;
void Number(int ret)
{
if (ret == 0)
{
cout<<" identical ";
}
}
int main()
{
int m = 0, n = 0;
cin >> m >> n;
int ret = m ^ n;// Same as 0, Different for 1
Number(ret);
}
Exercise 3 : Get the odd and even bits in the binary bit
// Get the odd and even bits in the binary bit
#include<iostream>
using namespace std;
int main()
{
// Print even numbers
int n = 0;
cin >> n;
for (int i = 31; i>=1; i-=2)
{
cout << ((n >> i) & 1);
}
cout << "```````````" << endl;
// Print odd digits
for (int i = 30; i >= 0; i -= 2)
{
cout << ((n >> i) & 1);
}
return 0;
}
Exercise 4 :++ Operation level ratio of * high

++ Operation level ratio of * high
Exercise five : Say a word and reverse as required
// Invert the words of a sentence , Punctuation is not inverted ,
// such as :i like beijing.
// After passing through the function, it becomes :beijing. like i
#include<iostream>
#include<string>
using namespace std;
void reverse(char *left,char* right )
{
while (left < right)
{
char tmp = 0;
tmp = *left;
*left = *right;
*right = tmp;
left++;
right--;
}
}
int main()
{
char arr[100] = {
0 };
cin.getline(arr,100);
// Three step flipping
//1 The whole string is flipped
int len = strlen(arr);
reverse(arr, arr + len - 1);
//2 Each word is in reverse order
char* start = arr;
while (*start)
{
char* end = start;
while (*end != ' ' && *end !='\0')
{
end++;
}
// One word in reverse order
reverse(start,end-1);
if (*end == ' ')
{
start = end + 1;
}
else
{
start = end;
}
}
cout << arr;
}
Exercise 6 : Long knowledge ,vs Smart

Knowledge points supplement : Small 、 Big end storage

Big endian byte order :
Put the contents of the low byte order of the data at the high address , The contents of the high byte are placed at the low address
Small endian byte order :
Put the low byte order contents of the data at the low address , The contents of the high byte order are placed at the high address

Exercise 7 : Digital storage
7.1 char c++ Conditions
#include<iostream>
using namespace std;
int main()
{
char a = -1;
signed char b = -1;
unsigned char c = -1;
cout <<"a=" << a << " b=" << b << " c=" << c;
}

7.2 char c Conditions
#include<stdio.h>
int main()
{
char a = -1;
//1000 0000 0000 0000 0000 0000 0000 0001
//1111 1111 1111 1111 1111 1111 1111 1111 -- Complement code ( Computer storage form )
signed char b = -1;
//1111 1111
unsigned char c = -1;
//0000 0000 0000 0000 0000 0000 1111 1111
//255
printf("a=%d\n",a);
printf("b=%d\n",b);
printf("c=%d\n",c);
}

7.3 %u Output char
#include<stdio.h>
int main()
{
char a = -128;
// Source code 1000 0000 0000 0000 0000 0000 1000 0000
// Complement code 1111 1111 1111 1111 1111 1111 1000 0000
printf("%u\n",a);//%u Unsigned 10 Hexadecimal integer
return 0;
}


#include<stdio.h>
int main()
{
int i = -20;
// Source code 1000 0000 0000 0000 0000 0000 0001 0100
// Complement code 1111 1111 1111 1111 1111 1111 1110 1100
unsigned int j = 10;
// Source code 0000 0000 0000 0000 0000 0000 0000 1010
printf("%d\n",i+j);
//1111 1111 1111 1111 1111 1111 1110 1100
//0000 0000 0000 0000 0000 0000 0000 1010
//1111 1111 1111 1111 1111 1111 1111 0110
// reduce 1, Take the inverse
//1111 1111 1111 1111 1111 1111 1111 0101
//1000 0000 0000 0000 0000 0000 0000 1010 —— -10
return 0;
}

7.4 Floating point storage
#include<stdio.h>
int main()
{
int n = 9;
float* pFloat = (float*)&n;
printf("n The value of is :%d\n",n);//9
printf("*pFloat The value of is :%f\n",*pFloat);// Floating point numbers are stored differently from integers
*pFloat = 9.0;
printf("num The value of is :%d\n",n);// Floating point numbers are stored differently from integers
printf("*pFloat The value of is :%f\n",*pFloat);//9.000000
return 0;
}

7.5
(-1)s*M*2E
(-1)^s The sign bit , When s=0,v Integers ; When s=1,v It's a negative number
M Represents a significant number , Greater than or equal to 1, Less than 2
2^E Indicates the index bit
101.0 == 1.01x2^2
s=0 M=1.01 E=2
-101.0 == -1.01x2^2
s=1 M=1.01 E=2
about 32 Floating point number of bits , The highest 1 Bits are sign bits s,
And then 8 Bits are exponents E, The rest 23 Bits are significant numbers M
边栏推荐
- Data URL
- Vcsa7u3c installation tutorial
- 直播预告 | 社交新纪元,共探元宇宙社交新体验
- Google Earth engine (GEE) -- Copernicus atmosphere monitoring (CAMs) global aerosol AOI near real-time observation data set
- Adobe Premiere Foundation (the last step of video subtitle adding) (6)
- mysql(17-触发器)
- mysql(17-课后练习题)
- [vulnhub range] janchow: 1.0.1
- 锐捷x32pro刷openwrt开启无线160MHz
- [Code] neural symbol generation machine
猜你喜欢

How to play the Dragon Boat Festival "immersive cloud Tour"? That is to say, it helps "live broadcast +" new scene landing

Low carbon data center construction ideas and future trends

How to transform digital transformation? Which way?

Seata安装Window环境

Introduction to ad18 device library import

Adobe Premiere基础-不透明度(蒙版)(十一)

Adobe Premiere Foundation (the last step of video subtitle adding) (6)

Chapter II data type (I)

mysql(17-触发器)

RK1126 新添加一个模块
随机推荐
nodejs-基本架构分析-解析引擎目录-插件安装-核心模块
Adobe Premiere基礎-工具使用(選擇工具,剃刀工具,等常用工具)(三)
vim常用快捷键
New trends and prospects of data center planning and design under the background of "double carbon"
lingo12软件下载及lingo语言入门资源
Low carbon data center construction ideas and future trends
Request header field XXXX is not allowed by access control allow headers in preflight response
mysql(17-触发器)
Seata installing the window environment
单纯形法代码求解(含超详细代码注释和整个流程图)
超级简单的课程设计ssm学生管理系统(含源码简单添加、删除、修改、查询操作)
Adobe Premiere基础特效(卡点和转场)(四)
Seata安装Window环境
Adobe Premiere Basic - tool use (select tools, rasoir tools, and other Common Tools) (III)
直播预告 | 社交新纪元,共探元宇宙社交新体验
How to transform digital transformation? Which way?
瑞芯微RK1126平台 平台移植libevent 交叉编译libevent
JS Touch
【代理】10分钟掌握正向代理和反向代理的本质区别
mysql(17-课后练习题)
