当前位置:网站首页>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");
}
}
}
边栏推荐
- Spatial interpretation | comprehensive analysis of spatial structure of primary liver cancer
- [unreal] key to open the door blueprint notes
- Introduction to go language
- 滲透測試的介紹和防範
- Tools used for Yolo object recognition and data generation
- Blender多镜头(多机位)切换
- Project practice, redis cluster technology learning (10)
- 三相逆变器离网控制——PR控制
- Brief analysis of edgedb architecture
- 【UE5】动画重定向:如何将幻塔人物导入进游戏玩耍
猜你喜欢
Off grid control of three-phase inverter - PR control
Applet development summary
Skywalking theory and Practice
JDBC review
ue虛幻引擎程序化植物生成器設置——如何快速生成大片森林
Bugkuctf-web24 (problem solving ideas and steps)
Eslint reports an error
2837xd 代码生成——StateFlow(3)
Inverter Simulink model -- processor in the loop test (PIL)
2837xd 代碼生成——補充(1)
随机推荐
[ue5] animation redirection: how to import magic tower characters into the game
三相逆变器离网控制——PR控制
Bugkuctf-web24 (problem solving ideas and steps)
Cmake command - Official Document
How does {} prevent SQL injection? What is its underlying principle?
Translation d30 (with AC code POJ 28:sum number)
Remember the use of add method once
Introduction et prévention des essais de pénétration
2837xd code generation - Supplement (1)
Blender多镜头(多机位)切换
【UE5】动画重定向:如何将幻塔人物导入进游戏玩耍
Image recognition - data annotation
It is the most difficult to teach AI to play iron fist frame by frame. Now arcade game lovers have something
Bookmark collection management software suspension reading and data migration between knowledge base and browser bookmarks
What wires are suitable for wiring on bread board?
Project practice, redis cluster technology learning (VIII)
Minimum number of C language
Project practice, redis cluster technology learning (6)
2837xd 代碼生成——補充(1)
Skywalking theory and Practice