C语言模拟练习(5)

程序代码:

#include<stdio.h>

//写一个函数返回函数二进制中1的个数
//比如:15 0000 1111 4个1
//程序原型
//int count_one_bits(unsigned int value){
// 返回1的个数
//}
//
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){   //计算函数二进制中1的个数
count++;
}
}
for(i=31;i>=0;i--){
printf("%d ",arr[i]);
}
printf("\n");
printf("%d 个1\n",count);
return 0;
}


//获取一个数二进制列中所有到偶数位和奇数位,分别输出二进制序列
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("偶数位:");
for(i=31;i>=0;i-=2){
printf("%d ",arr[i]);
}
printf("\n");
printf("奇数位:");
for(i=30;i>=0;i-=2){
printf("%d ",arr[i]);
}
printf("\n");
}
//输出整数的每一位
void output_bit_integer(unsigned int value){
printf("%d\n",value);
int arr[10]={0};
int count = 0;
while(value!= 0){  //将整数的每一位存储至数值中
arr[count] = value%10;
value = value/10;
++count;
}
printf("bit:");
--count;
for(;count>=0;--count){   //打印数组中的有效元素
printf("%d ",arr[count]);
}
printf("\n");
}
//编程实现:
//俩个int(32位)整数m和n的二进制表达中,有多少个位(bit)不同
//输入列子
//1999 2299
//输出列子: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();
}

检测结果:

[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****************
偶数位: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****************
value = 1999   value1 =  2299
different:7

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页