鱼C论坛

 找回密码
 立即注册
查看: 1039|回复: 9

[已解决]求大佬们解释下这个题

[复制链接]
发表于 2020-9-11 09:51:58 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
小弟刚刚学C,遇到这个题目看不懂,求大佬们帮忙并解释下
#include <stdio.h>
void set_flag(int* flag_holder, int flag_position);
int check_flag(int flag_holder, int flag_position);
int main(int argc, char* argv[])
{
  int flag_holder = 0;
  int i;
  set_flag(&flag_holder, 3);
  set_flag(&flag_holder, 16);
  set_flag(&flag_holder, 31);
  for(i=31; i>=0; i--){

    printf("%d", check_flag(flag_holder, i));
    if(i%4 == 0){
      
      printf(" ");
}
}
printf("\n");
return 0;
}
要求写出set_flag 和check_flag 的code
运行后结果如图
Capture.JPG

You can think of the set_flag function as taking an integer and making sure that the nth bit is a 1. The check_flag
function simply returns an integer that is zero when the nth bit is zero and 1 when it is 1. You may find the shifting
operators “<<”, and “>>” helpful as well as the bitwise operations & and |. If you find yourself using multiplication
or division in your solution then you are doing it wrong.
最佳答案
2020-9-11 13:21:52
本帖最后由 sunrise085 于 2020-9-11 14:39 编辑
You can think of the set_flag function as taking an integer and making sure that the nth bit is a 1. The check_flag
function simply returns an integer that is zero when the nth bit is zero and 1 when it is 1. You may find the shifting
operators “<<”, and “>>” helpful as well as the bitwise operations & and |. If you find yourself using multiplication
or division in your solution then you are doing it wrong.

题目的意思:
set_flag 函数将一个 int 类型数值的第 n 个字节位设置为1。check_flag 函数简单地返回一个 int 类型数字的第 n 个字节位是1还是0,。
你应该用到左移操作符"<<"和右移操作符">>",同时可能还需要用到位逻辑运算符 & 和 | 。如果你的程序中用到了乘/除,那说明你的程序编写错了。


题目就是让你用位操作符和位运算符来完成为设置和位查询功能

  1. #include <stdio.h>
  2. void set_flag(int* flag_holder, int flag_position);    // 声明函数 set_flag
  3. int check_flag(int flag_holder, int flag_position);    // 声明函数 check_flag

  4. int main(int argc, char* argv[])                       // 主函数
  5. {
  6.   int flag_holder = 0;                                 // 给 flag_holder 整型变量赋初值
  7.   int i;
  8.   set_flag(&flag_holder, 3);                           // 调用 set_flag 函数
  9.   set_flag(&flag_holder, 16);                          // 调用 set_flag 函数
  10.   set_flag(&flag_holder, 31);                          // 调用 set_flag 函数
  11.   for(i=31; i>=0; i--)                                 // 循环 31 次调用 printf() 输出
  12.   {
  13.     printf("%d", check_flag(flag_holder, i));          // 在 printf() 中调用函数 check_flag 函数的返回值
  14.     if(i%4 == 0)                                       // 以下四行的作用是:输出四个值后,输出一个空格
  15.         {
  16.       
  17.       printf(" ");
  18.         }
  19.   }
  20.   printf("\n");                                        // 输出一个回车
  21.   return 0;
  22. }


  23. // 定义 set_flag 函数
  24. void set_flag(int* flag_holder, int flag_position)
  25. {
  26.     int temp = 1;
  27.     temp <<= flag_position;               //将temp 左移 flag_position 位,即将 1 左移 flag_position 位
  28.     *flag_holder =* flag_holder|temp;     //原数值与 temp 进行位或运算,即修改第 flag_position 位为1
  29. }


  30. // 以下需要定义 check_flag 函数
  31. int check_flag(int flag_holder, int flag_position)
  32. {
  33.     unsigned int temp = flag_holder;       //将flag_holder 改为无符号整型
  34.     temp >>= flag_position;         //然后右移flag_position位,即将第 flag_position 位移动到最右边
  35.     return temp&1;                  //将tenp与1进行位与运算,得到的就是第 flag_position  位的值
  36. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-9-11 10:42:33 | 显示全部楼层
本帖最后由 风过无痕1989 于 2020-9-11 11:13 编辑


暈~~晕~~晕~~,是让我写这两个函数呀?这题目出的太。。。。。(此处省略16个字)

set_flag 可以是个空函数,然后用 cheak_flag 函数硬凑你的那个答案,你没有理由说我错吧?

这样的题目,你们自己玩吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-11 10:58:36 | 显示全部楼层
  1. #include <stdio.h>
  2. void set_flag(int* flag_holder, int flag_position);    // 声明函数 set_flag
  3. int check_flag(int flag_holder, int flag_position);    // 声明函数 check_flag

  4. int main(int argc, char* argv[])                       // 主函数
  5. {
  6.   int flag_holder = 0;                                 // 给 flag_holder 整型变量赋初值
  7.   int i;
  8.   set_flag(&flag_holder, 3);                           // 调用 set_flag 函数
  9.   set_flag(&flag_holder, 16);                          // 调用 set_flag 函数
  10.   set_flag(&flag_holder, 31);                          // 调用 set_flag 函数
  11.   for(i=31; i>=0; i--)                                 // 循环 31 次调用 printf() 输出
  12.   {
  13.     printf("%d", check_flag(flag_holder, i));          // 在 printf() 中调用函数 check_flag 函数的返回值
  14.     if(i%4 == 0)                                       // 以下四行的作用是:输出四个值后,输出一个空格
  15.         {
  16.       
  17.       printf(" ");
  18.         }
  19.   }
  20.   printf("\n");                                        // 输出一个回车
  21.   return 0;
  22. }


  23. // 以下需要定义 set_flag 函数
  24. void set_flag(int* flag_holder, int flag_position)
  25. {
  26.   ......
  27. }


  28. // 以下需要定义 check_flag 函数
  29. int check_flag(int flag_holder, int flag_position)
  30. {
  31.   ......
  32. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-9-11 11:12:30 | 显示全部楼层
本帖最后由 cinmay 于 2020-9-11 11:13 编辑
风过无痕1989 发表于 2020-9-11 10:42
你声明了两个函数,却没有定义这两个函数


这两个函数需要自己写出来,我不会
我在看小甲鱼的视频,现学赶不上:(
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-11 11:14:27 | 显示全部楼层
cinmay 发表于 2020-9-11 11:12
这两个函数需要自己写出来,我不会
我在看小甲鱼的视频,现学赶不上:(

前面 2楼 重新编辑已经回复你了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-11 13:21:52 | 显示全部楼层    本楼为最佳答案   
本帖最后由 sunrise085 于 2020-9-11 14:39 编辑
You can think of the set_flag function as taking an integer and making sure that the nth bit is a 1. The check_flag
function simply returns an integer that is zero when the nth bit is zero and 1 when it is 1. You may find the shifting
operators “<<”, and “>>” helpful as well as the bitwise operations & and |. If you find yourself using multiplication
or division in your solution then you are doing it wrong.

题目的意思:
set_flag 函数将一个 int 类型数值的第 n 个字节位设置为1。check_flag 函数简单地返回一个 int 类型数字的第 n 个字节位是1还是0,。
你应该用到左移操作符"<<"和右移操作符">>",同时可能还需要用到位逻辑运算符 & 和 | 。如果你的程序中用到了乘/除,那说明你的程序编写错了。


题目就是让你用位操作符和位运算符来完成为设置和位查询功能

  1. #include <stdio.h>
  2. void set_flag(int* flag_holder, int flag_position);    // 声明函数 set_flag
  3. int check_flag(int flag_holder, int flag_position);    // 声明函数 check_flag

  4. int main(int argc, char* argv[])                       // 主函数
  5. {
  6.   int flag_holder = 0;                                 // 给 flag_holder 整型变量赋初值
  7.   int i;
  8.   set_flag(&flag_holder, 3);                           // 调用 set_flag 函数
  9.   set_flag(&flag_holder, 16);                          // 调用 set_flag 函数
  10.   set_flag(&flag_holder, 31);                          // 调用 set_flag 函数
  11.   for(i=31; i>=0; i--)                                 // 循环 31 次调用 printf() 输出
  12.   {
  13.     printf("%d", check_flag(flag_holder, i));          // 在 printf() 中调用函数 check_flag 函数的返回值
  14.     if(i%4 == 0)                                       // 以下四行的作用是:输出四个值后,输出一个空格
  15.         {
  16.       
  17.       printf(" ");
  18.         }
  19.   }
  20.   printf("\n");                                        // 输出一个回车
  21.   return 0;
  22. }


  23. // 定义 set_flag 函数
  24. void set_flag(int* flag_holder, int flag_position)
  25. {
  26.     int temp = 1;
  27.     temp <<= flag_position;               //将temp 左移 flag_position 位,即将 1 左移 flag_position 位
  28.     *flag_holder =* flag_holder|temp;     //原数值与 temp 进行位或运算,即修改第 flag_position 位为1
  29. }


  30. // 以下需要定义 check_flag 函数
  31. int check_flag(int flag_holder, int flag_position)
  32. {
  33.     unsigned int temp = flag_holder;       //将flag_holder 改为无符号整型
  34.     temp >>= flag_position;         //然后右移flag_position位,即将第 flag_position 位移动到最右边
  35.     return temp&1;                  //将tenp与1进行位与运算,得到的就是第 flag_position  位的值
  36. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-11 14:33:07 | 显示全部楼层
sunrise085 发表于 2020-9-11 13:21
题目的意思:
set_flag 函数讲一个 int 类型数值的第 n 个字节位设置为1。check_flag 函数简单地返回一 ...

他的求助标题是解释下这个题,最后变成了帮他写两个函数,我感觉被骗了。

还是你心好,达成了他的目的,最佳非你莫属
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-9-11 19:44:51 | 显示全部楼层
感谢两位的解答,现在清楚很多了,你们的帮助对初学者来说太重要了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-9-11 19:46:55 | 显示全部楼层
风过无痕1989 发表于 2020-9-11 14:33
他的求助标题是解释下这个题,最后变成了帮他写两个函数,我感觉被骗了。

还是你心好,达成了他的目的 ...

非常感谢你的帮助。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-12 02:17:33 | 显示全部楼层
cinmay 发表于 2020-9-11 19:46
非常感谢你的帮助。

下次发帖子请求帮助时,注意主要问题与次要问题,标题是主要问题,次要问题可在帖子中互动时提出来,这样首先解决的就是你的主要问题。

另,我也是初学者,每次回复问题,我当是对我的一次小考,以此来加深自己对概念的理解
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-6 18:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表