课后习题21

#include<stdlib.h>
//两个int(32位)整数m和n的二进制表达中,有多少个位(bit)不同?
#include<stdio.h>
int main(){
 int a = 1999;
 int b = 2299;
 int i;
 int count = 0;
 for (i = 0; i < 32; i++){
  if (((a >> i) & 1) == 1 && ((b >> i) & 1) != 1 ||
   ((a >> i) & 1) == 0 && ((b >> i) & 1) != 0){
   count++;
   }
 }
 printf("count=%d", count);
 system("pause");
 return 0;
}

#include<stdio.h>
#include<stdlib.h>
/*int main(){// 写一个函数打印arr数组的内容,不使用数组下标,使用指针。
 int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
 int i = 0;
 int *p = arr;
 for (i = 0; i < 10; i++){
  printf("%d ", *(p + i));
 }
 system("pause");
 return 0;
}
int main()
{
 int a = 0x11223344;
 char *pc = (char*)&a;
 *pc = 0;
 printf("%x\n", a);
 system("pause");
 return 0;
}
int main()
{
 int arr[] = { 1, 2, 3, 4, 5 };
 short *p = (short*)arr;
 int i = 0;
 for (i = 0; i<4; i++)
 {
  *(p + i) = 0;
 }
 for (i = 0; i<5; i++)
 {
  printf("%d ", arr[i]);
 }
 system("pause");
 return 0;
}
#include<stdio.h>
int i;
int main()
{
 i--;
 if (i > sizeof(i))
 {
  printf(">\n");
 }
 else
 {
  printf("<\n");
 }
 system("pause");
 return 0;
}*/
/*2. 写一个函数返回参数二进制中 1 的个数(牛客网的OJ链接)
例如: 15    0000 1111    4 个 1
程序原型:
int count_one_bits(unsigned int value)
{
 // 返回 1的位数
}
int number(int a,int b){
 int count = 0;
 for (int i = 0; i < 8; i++){
  if (a&b == 1){
   count++;
   b <- i++;
  }
 }
 return count;
}
int main(){
 int a = 15;
 int b = 1;
 number(15, 1);
 printf("%d", number(15, 1));
 system("pause");
 return 0;
}*/
//1. 不允许创建临时变量,交换两个整数的内容
#include<stdio.h>
int main(){
 int a = 20;
 int b = 30;
 a = a^b;
 b = a^b;
 a = a^b;
 printf("a=%d\n", a);
 printf("b=%d\n", b);
 system("pause");
 return 0;
}
//. 获取一个整数二进制序列中所有的偶数位和奇数位。
//要求:分别打印出二进制序列。
#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int main(){
 int num;
 int i;
 printf("input num:");
 scanf("%d", &num);
 printf("偶数\n");
 for (i = 31; i >= 1; i -= 2){
  if ((num >> i) & 1){
   printf("%d ", 1);
  }
  else
   printf("%d ",0);
 }
 printf("\n");
 printf("奇数\n");
 for (i = 30; i >= 0; i -= 2){
  if ((num >> i) & 1){
   printf("%d ", 1);
  }
  else
   printf("%d ", 0);
 }
 system("pause");
 return 0;
}
 
 
posted @ 2019-12-06 15:12  哈哈,呵呵,嘿嘿  阅读(190)  评论(0)    收藏  举报