Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
CSDN 技术社区
skill_tree_c
提交
48d47ca5
S
skill_tree_c
项目概览
CSDN 技术社区
/
skill_tree_c
通知
24
Star
4
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
skill_tree_c
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
“3d769b97ff51703e21c01b5c7bc5c365c658259a”上不存在“paddle/fluid/git@gitcode.net:RobotFutures/Paddle.git”
提交
48d47ca5
编写于
10月 21, 2021
作者:
M
Mars Liu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fixed a settings error
上级
e85b1647
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
75 addition
and
0 deletion
+75
-0
data/3.C语言高阶/3.位运算/2.二进制数/count.md
data/3.C语言高阶/3.位运算/2.二进制数/count.md
+75
-0
未找到文件。
data/3.C语言高阶/3.位运算/2.二进制数/count.md
0 → 100644
浏览文件 @
48d47ca5
# 计算位数
计算整数的二进制形式有多少位。
## template
```
c
#include <stdio.h>
$
code
int
main
(
char
**
args
){
printf
(
"bits(%d) has %d bits
\n
"
,
100
,
bits
(
100
));
printf
(
"bits(%d) has %d bits
\n
"
,
32
,
bits
(
32
));
printf
(
"bits(%d) has %d bits
\n
"
,
10
,
bits
(
10
));
printf
(
"bits(%d) has %d bits
\n
"
,
25
,
bits
(
25
));
printf
(
"bits(%d) has %d bits
\n
"
,
7
,
bits
(
7
));
return
0
;
}
```
## 答案
```
c
unsigned
int
bits
(
unsigned
int
x
){
unsigned
int
n
=
0
;
while
(
x
){
x
>>=
1
;
n
++
;
}
return
n
;
}
```
## 选项
### 位移计算没有保存
```
c
unsigned
int
bits
(
unsigned
int
x
){
unsigned
int
n
=
0
;
while
(
x
){
x
>>
1
;
n
++
;
}
return
n
;
}
```
### 没有括号导致流程错误
```
c
unsigned
int
bits
(
unsigned
int
x
){
unsigned
int
n
=
0
;
while
(
x
)
x
>>=
1
;
n
++
;
return
n
;
}
```
### 运算符错误
```
c
unsigned
int
bits
(
unsigned
int
x
){
unsigned
int
n
=
0
;
while
(
x
){
x
<<
1
;
n
++
;
}
return
n
;
}
```
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录