当前位置:网站首页>C knowledge exercise

C knowledge exercise

2022-06-10 19:11:00 Fate friend I

In front of ++, Add one before returning
After ++, Return first and then add one
 Insert picture description here

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;
}

 Insert picture description here

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

 Insert picture description here

++ 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

 Insert picture description here

Knowledge points supplement : Small 、 Big end storage

 Insert picture description here

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

 Insert picture description here

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;
}

 Insert picture description here

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);
}

 Insert picture description here

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;
}

 Insert picture description here

 Insert picture description here

#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;
}

 Insert picture description here

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;
}

 Insert picture description here

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

原网站

版权声明
本文为[Fate friend I]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101815406056.html