当前位置:网站首页>Operator exercises
Operator exercises
2022-07-02 10:10:00 【Xiao Liu H】
Catalog
2 Enter an integer n , Output the number 32 In bit binary representation 1 The number of .
3 Find the number of different bits in two binary numbers
4 Print odd and even bits of integer binary
5 The result of the following code is
6 Judge integer parity ( Multiple input scenarios )
7 Judge whether it's a vowel or a consonant ( Multiple input scenarios )
1 Knowledge point
(1)b = ++c, c++, ++a, a++ // The comma expression has the lowest priority , Let's start here b=++c, b Get is ++c After the results of the ,b=++c And the following form a comma expression , Calculated from left to right .
(2) Global variables When not initialized It will be initialized to... By default 0.
(3) About expression evaluation , Be careful Want to consider Implicit type conversion 、 Arithmetic conversion 、 The priority of the operator associativity Order of evaluation
2 Enter an integer n , Output the number 32 In bit binary representation 1 The number of .
Negative numbers are represented by complements . requirement : Write a function Returns the binary value of the parameter 1 The number of .
Code 1 Exhibition :( Be careful : Here is the binary of complement in memory 1 The number of )
#include <stdio.h>
int count_number_of_1(unsigned int n)
{
int count = 0;
while (n)
{
if (n % 2 == 1)
{
count++;
n = n / 2;
}
}
return count;
}
int main()
{
int a =-1;
int ret = count_number_of_1(a);
printf("%d", ret);
return 0;
}Be careful : The function says unsigned int ( Unsigned integer int)
Code parsing :
7 The binary sequence of is 00000000000000000000000000000111, When 7%2=1, This 1 Is the last bit of binary ,7/2=3, This 3 yes 7 The binary sequence of moves one displayed digit to the right ,00000000000000000000000000000011.( This line of thought, , When a decimal number , To know every digit , We usually go first %10, Divided by 10 Follow this cycle )
Code 2 Exhibition :
#include <stdio.h>
int count_number_of_1(int n)
{
int count = 0;
int i = 0;
for (i = 0; i < 32; i++)
{
if ((n & 1) == 1)
{
count++;
}
n = n >> 1;
}
return count;
}
int main()
{
int a = -1;
int ret = count_number_of_1(a);
printf("%d", ret);
return 0;
}Code 3 Exhibition :
#include <stdio.h>
int count_number_of_1(int n)
{
int count = 0;
while (n)
{
n = n & (n - 1);
count++;
}
return count;
}
int main()
{
int a = -1;
int ret = count_number_of_1(a);
printf("%d", ret);
return 0;
}Code understanding :
m&(m-1) Will remove one binary sequence from the right 1 ( 0111&0110=0110 0110&0101=0100)
( Remove one at a time 1, How many times , Just how many 1)
( This is the best answer , There are several 1, Just cycle a few times .)
3 Find the number of different bits in two binary numbers
Two int(32 position ) Integers m and n In the binary representation of , How many bits (bit) Different
Code 1 Exhibition :
#include <stdio.h>
int count_diff_bit(int m, int n)
{
int count = 0;
int i = 0;
for (i = 0; i < 32; i++)
{
if ((m & 1) != (n & 1))
{
count++;
}
m >>= 1;
n >>= 1;
}
return count;
}
int main()
{
int a = -1;
int b = 1;
int ret = count_diff_bit(a, b);
printf("%d", ret);
return 0;
}Code 2 Exhibition :
#include <stdio.h>
int count_diff_bit(int m, int n)
{
int count = 0;
int i = m ^ n;
while (i)
{
i = i & (i - 1);
count++;
}
return count;
}
int main()
{
int a = -1;
int b = 1;
int ret = count_diff_bit(a, b);
printf("%d", ret);
return 0;
}Exclusive or Same as 0, Different for 1., The result goes back to finding a binary sequence 1 The number of .
4 Print odd and even bits of integer binary
Get all even and odd bits in an integer binary sequence , Print out the binary sequence separately
#include <stdio.h>
void print(int m)
{
int i = 0;
for (i = 30; i >= 0; i -= 2)
{
printf("%d ", (m >> i) & 1);
}
printf("\n");
for (i = 31; i >= 1; i -= 2)
{
printf("%d ", (m >> i) & 1);
}
}
int main()
{
int a = 5;
scanf("%d", &a);
print(a);
return 0;
}5 The result of the following code is
#include <stdio.h>
int i;
int main()
{
i--;
if (i > sizeof(i))
{
printf(">\n");
}
else
{
printf("<\n");
}
return 0;
}A.> B.< C. No output D. There is a problem with the procedure
analysis :
C In language ,0 For false , Not 0 It's true . Global variables , When no initial value is given , Compiling it initializes it to... By default 0.
i The initial value of 0,i-- result -1,i For shaping ,sizeof(i) seek i The type size is 4, According to this analysis , The result should be B, however sizeof The return value type of is actually an unsigned integer , Therefore, the compiler will automatically set the left i Automatically convert to unsigned data ,-1 The corresponding unsigned integer is a very large number , exceed 4 perhaps 8, Therefore, it should be selected in practice A
sizeof Operands The result type of calculation is size_t Unsigned integer unsigned int In this question int >unsigned int First of all, we should put int type ( Perform arithmetic conversion in implicit type conversion ) convert to unsigned int Type is OK When -1 When it is considered an unsigned integer (-1 The complement of is 32 individual 1), Original code Inverse code The complement should be the same , that -1 The unsigned integer of is obviously very large , Greater than 4, So we should choose A
6 Judge integer parity ( Multiple input scenarios )
Enter any integer from the keyboard ( Range -231~231-1), Program to judge its parity . Input description : Multi group input , Each line of input includes an integer . Output description : Enter... For each line , The output number is odd (Odd) Or even (Even).
Code display :
#include <stdio.h>
int main()
{
int a = 0;
while ((scanf("%d", &a)) != EOF)
{
if (a % 2 == 0)
{
printf("Even\n");
}
else
{
printf("Odd\n");
}
}
return 0;
}scanf Read failed , It will return EOF
7 Judge whether it's a vowel or a consonant ( Multiple input scenarios )
Input description :
Multi group input , Enter one letter per line .
Output description :
Enter... For each group , Output as a line , If the input letter is a vowel ( Include case ), Output “Vowel”, If the input letter is a non vowel , Output “Consonant”.
#include <stdio.h>
int main()
{
char arr[] = { 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U' };
char ch = 0;
while ((scanf("%c", &ch) != EOF))
{
int i = 0;
for (i = 0; i < 10; i++)
{
if (ch == arr[i])
{
printf("Vowel\n");
break;
}
}
if (i == 10)
{
printf("Consonant\n");
}
getchar();
}
}Be careful :
Get on the keyboard ch When it comes to type , Will be able to \n Also enter into , So want to use getchar() Clean buffer
Code 2 Exhibition :
#include <stdio.h>
int main()
{
char arr[] = { 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U' };
char ch = 0;
while ((scanf("%c\n", &ch) != EOF))
{
int i = 0;
for (i = 0; i < 10; i++)
{
if (ch == arr[i])
{
printf("Vowel\n");
break;
}
}
if (i == 10)
{
printf("Consonant\n");
}
}
}Code 3 Exhibition :
#include <stdio.h>
int main()
{
char arr[] = { 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U' };
char ch = 0;
while ((scanf(" %c", &ch) != EOF))
{
int i = 0;
for (i = 0; i < 10; i++)
{
if (ch == arr[i])
{
printf("Vowel\n");
break;
}
}
if (i == 10)
{
printf("Consonant\n");
}
}
}边栏推荐
- In SQL injection, why must the ID of union joint query be equal to 0
- 2.14 is it Valentine's day or Valentine's day when the mainstream market continues to fluctuate and wait for changes?
- Alibaba cloud Prometheus monitoring service
- Mixed development of uni app -- Taking wechat applet as an example
- MySQL index
- ue虚幻引擎程序化植物生成器设置——如何快速生成大片森林
- Error reporting on the first day of work (error reporting when Nessus installs WinPcap)
- Skywalking theory and Practice
- [200 Shengxin literatures] 96 joint biomarkers of immune checkpoint inhibitor response in advanced solid tumors
- How much is it to develop a system software in Beijing, and what funds are needed to develop the software
猜你喜欢

MySQL default transaction isolation level and row lock

2837xd code generation - Summary
![[illusory] weapon slot: pick up weapons](/img/a7/1e395fc9cdfd0359e7ae4d2313290d.png)
[illusory] weapon slot: pick up weapons

Junit4 runs MVN test test suite upgrade scheme

Sil/pil test of matlab code generation
What is the relationship between realizing page watermarking and mutationobserver?

2837xd code generation - stateflow (4)
![[ue5] blueprint making simple mine tutorial](/img/87/d0bec747a6b6276d63a315f88745ec.png)
[ue5] blueprint making simple mine tutorial

Off grid control of three-phase inverter - PR control

Vs+qt set application icon
随机推荐
【虚幻】按键开门蓝图笔记
Web security and defense
C language programming problems
XA Transaction SQL Statements
Bugkuctf-web21 (detailed problem solving ideas and steps)
2837xd code generation module learning (2) -- ADC, epwm module, timer0
三相并网逆变器PI控制——离网模式
Error reporting on the first day of work (incomplete awvs unloading)
Mysql索引
Judging right triangle in C language
2837xd 代码生成——补充(2)
How does {} prevent SQL injection? What is its underlying principle?
Alibaba cloud SLS log service
2837xd code generation - stateflow (1)
2837xd code generation - stateflow (2)
[200 Shengxin literatures] 95 multiomics exploration TNBC
Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer
Because of hard work, the fruit goes with fate
2837xd code generation module learning (4) -- idle_ task、Simulink Coder
Spatial interpretation | comprehensive analysis of spatial structure of primary liver cancer