当前位置:网站首页>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");
}
}
}
边栏推荐
- 2837xd code generation - Supplement (3)
- Eslint reports an error
- 渗透测试的介绍和防范
- [ue5] two implementation methods of AI random roaming blueprint (role blueprint and behavior tree)
- 2837xd code generation module learning (1) -- GPIO module
- [ue5] blueprint making simple mine tutorial
- Image recognition - Data Cleaning
- Remember a simple Oracle offline data migration to tidb process
- 2837xd code generation - Supplement (2)
- Binary and decimal system of C language
猜你喜欢
Bugkuctf-web24 (problem solving ideas and steps)
MySQL transaction
Mysql索引
[unreal] key to open the door blueprint notes
三相并网逆变器PI控制——离网模式
Illusion -- Animation blueprint, state machine production, character walking, running and jumping action
2837xd code generation module learning (1) -- GPIO module
Alibaba cloud SLS log service
2837xd code generation module learning (2) -- ADC, epwm module, timer0
滲透測試的介紹和防範
随机推荐
Is the C language too fat
In SQL injection, why must the ID of union joint query be equal to 0
2837xd代码生成模块学习(4)——idle_task、Simulink Coder
Image recognition - Data Acquisition
【虚幻】自动门蓝图笔记
Project practice, redis cluster technology learning (VII)
2837xd 代码生成——StateFlow(1)
How to handle error logic gracefully
ZK configuration center -- configuration and use of config Toolkit
How does {} prevent SQL injection? What is its underlying principle?
C language: making barrels
2837xd代码生成模块学习(3)——IIC、eCAN、SCI、Watchdog、eCAP模块
Ckeditor 4.10.1 upload pictures to prompt "incorrect server response" problem solution
Kinect DK obtains color RGB images in cv:: mat format (used in openpose)
UE5——AI追逐(蓝图、行为树)
2837xd code generation module learning (4) -- idle_ task、Simulink Coder
三相并网逆变器PI控制——离网模式
The road is blocked and long, and the line is coming
三相逆变器离网控制——PR控制
[illusory] weapon slot: pick up weapons