C language simulation exercises (5)

code:

#include<stdio.h>

//Write a function to return the number of 1s in the function binary
//For example: 15 0000 1111 4 1s
//Program prototype
//int count_one_bits(unsigned int value){
// Return 1s number
//}
//
int count_one_bits(unsigned int value){
int i = 0;
int arr[32]={0};
int count = 0;
for(;i<32;i++){
arr[i] = value&1 ;
value = value>>1;
if(arr[i] == 1){ //count the number of 1s in the function binary
count++;
}
}
for(i=31;i>=0;i--){
printf ("%d ",arr[i]);
}
printf("\n");
printf("%d 1\n",count);
return 0;
}


//Get all to even numbers in a binary column Bit and odd bits, output binary sequence respectively
void get_odd_and_even(unsigned int value){
int i = 0;
int arr[32] = {0};
int count = 0;
for(;i<32;i++){
arr[i] = value&1;
value = value>>1;
}
printf("Even digits: ");
for (i=31;i>=0;i-=2){
printf("%d ",arr[i]);
}
printf("\n");
printf("odd digits: ");
for(i =30;i>=0;i-=2){
printf("%d ",arr[i]);
}
printf("\n");
}
//Output each bit of the integer
void output_bit_integer(unsigned int value){
printf("%d\n",value);
int arr[10]={0};
int count = 0;
while(value!= 0){ //Store each bit of the integer into the value
arr[count] = value%10;
value = value/10;
++count;
}
printf("bit:");
--count;
for(;count>=0;--count){ //print valid elements in the array
printf("%d ",arr[count]);
}
printf("\n");
}
//programming implementation:
/ /How many bits (bits) are different in the binary representation of two int (32-bit) integers m and n
//input column
//1999 2299
//output column: 7
void bit_of_value(unsigned int value, unsigned int value1) {
int i = 0;
int arr[32]={0};
int arr1[32]={0};
int count = 0;
printf("value = %d value1 = %d\n",value,value1) ;
for(;i<32;i++){
arr[i] = value&1;
value = value>>1;
}
for(i=0;i<32;i++){
arr1[i] = value1&1;
value1 = value1> >1;
}
for(i=0;i<32;i++){
if(arr[i]!=arr1[i]){
count++;
}
}
printf("different:");
printf("%d\n",count);
}
void textcount_one_bits(){
printf("\n*************%s****************\n",__FUNCTION__);
count_one_bits(15);
}
void textget_odd_and_even(){
printf("\n*************%s****************\n",__FUNCTION__);
get_odd_and_even(15);
}
void textoutput_bit_integer(){
printf("\n*************%s****************\n",__FUNCTION__);
output_bit_integer(15);
}
void textbit_of_value(){
printf("\n*************%s****************\n",__FUNCTION__);
bit_of_value(1999,2299);
}
int main(){
textcount_one_bits();
textget_odd_and_even();
textoutput_bit_integer();
textbit_of_value();
}

Test results:

[chaiyandong@localhost exec_about_c]$ ./day06


*************textcount_one_bits****************
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 
4 个1


*************textget_odd_and_even****************
偶数位:0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 
奇数位:0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 


*************textoutput_bit_integer****************
15
bit:1 5 


*************textbit_of_value****************
different:7
[chaiyandong@localhost exec_about_c]$ vim day06.c
[chaiyandong@localhost exec_about_c]$ make clear
rm -r day06
[chaiyandong@localhost exec_about_c]$ make
gcc -g -o day06 day06.c
[chaiyandong@localhost exec_about_c]$ ./day06


*************textcount_one_bits****************
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 
4 1


*************textget_odd_and_even****************
even bits: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 
Odd bit: 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 


*************textoutput_bit_integer*** *************
15
bit: 1 5 


*************textbit_of_value****************
value = 1999 value1 = 2299
different: 7

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324515295&siteId=291194637