Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
CSDN 技术社区
skill_tree_c
提交
62a1babb
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看板
提交
62a1babb
编写于
10月 21, 2021
作者:
M
Mars Liu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add oct
上级
b5568f5f
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
197 addition
and
5 deletion
+197
-5
data/3.C语言高阶/3.位运算/1.位和字节/check.md
data/3.C语言高阶/3.位运算/1.位和字节/check.md
+3
-3
data/3.C语言高阶/3.位运算/2.二进制数/config.json
data/3.C语言高阶/3.位运算/2.二进制数/config.json
+2
-1
data/3.C语言高阶/3.位运算/2.二进制数/count.json
data/3.C语言高阶/3.位运算/2.二进制数/count.json
+7
-0
data/3.C语言高阶/3.位运算/2.二进制数/cout.md
data/3.C语言高阶/3.位运算/2.二进制数/cout.md
+75
-0
data/3.C语言高阶/3.位运算/3.八进制/config.json
data/3.C语言高阶/3.位运算/3.八进制/config.json
+2
-1
data/3.C语言高阶/3.位运算/3.八进制/oct.json
data/3.C语言高阶/3.位运算/3.八进制/oct.json
+7
-0
data/3.C语言高阶/3.位运算/3.八进制/oct.md
data/3.C语言高阶/3.位运算/3.八进制/oct.md
+101
-0
未找到文件。
data/3.C语言高阶/3.位运算/1.位和字节/check.md
浏览文件 @
62a1babb
...
@@ -23,7 +23,7 @@ int main(char** args){
...
@@ -23,7 +23,7 @@ int main(char** args){
```
c
```
c
int
check
(
int
x
)
{
unsigned
int
check
(
unsigned
int
x
)
{
if
(
x
<
2
){
if
(
x
<
2
){
return
0
;
return
0
;
}
}
...
@@ -48,7 +48,7 @@ int check(int x) {
...
@@ -48,7 +48,7 @@ int check(int x) {
```
c
```
c
int
check
(
int
x
)
{
unsigned
int
check
(
unsigned
int
x
)
{
return
x
&
(
x
-
1
)
==
0
;
return
x
&
(
x
-
1
)
==
0
;
}
}
...
@@ -57,7 +57,7 @@ int check(int x) {
...
@@ -57,7 +57,7 @@ int check(int x) {
### 边界条件处理的不对
### 边界条件处理的不对
```
c
```
c
int
check
(
int
x
)
{
unsigned
int
check
(
unsigned
int
x
)
{
for
(
int
value
=
x
;
value
>
0
;
value
=
value
/
2
)
{
for
(
int
value
=
x
;
value
>
0
;
value
=
value
/
2
)
{
if
((
value
%
2
)
!=
0
){
if
((
value
%
2
)
!=
0
){
return
0
;
return
0
;
...
...
data/3.C语言高阶/3.位运算/2.二进制数/config.json
浏览文件 @
62a1babb
...
@@ -6,5 +6,5 @@
...
@@ -6,5 +6,5 @@
"C语言"
"C语言"
],
],
"children"
:
[],
"children"
:
[],
"export"
:
[]
"export"
:
[
"count.json"
]
}
}
\ No newline at end of file
data/3.C语言高阶/3.位运算/2.二进制数/count.json
0 → 100644
浏览文件 @
62a1babb
{
"type"
:
"code_options"
,
"author"
:
"刘鑫"
,
"source"
:
"check.md"
,
"exercise_id"
:
"277ffa9a36b648c8a55a512c7e5daf99"
}
\ No newline at end of file
data/3.C语言高阶/3.位运算/2.二进制数/cout.md
0 → 100644
浏览文件 @
62a1babb
# 计算位数
计算整数的二进制形式有多少位。
## 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
data/3.C语言高阶/3.位运算/3.八进制/config.json
浏览文件 @
62a1babb
...
@@ -6,5 +6,5 @@
...
@@ -6,5 +6,5 @@
"C语言"
"C语言"
],
],
"children"
:
[],
"children"
:
[],
"export"
:
[]
"export"
:
[
"oct.json"
]
}
}
\ No newline at end of file
data/3.C语言高阶/3.位运算/3.八进制/oct.json
0 → 100644
浏览文件 @
62a1babb
{
"type"
:
"code_options"
,
"author"
:
"刘鑫"
,
"source"
:
"oct.md"
,
"exercise_id"
:
"b15add5a05e84ecdbdf862dcfa722b30"
}
\ No newline at end of file
data/3.C语言高阶/3.位运算/3.八进制/oct.md
0 → 100644
浏览文件 @
62a1babb
# 八进制转换
将正整数输出为八进制形式的字符串。现已有申请内存的
`create_buffer`
函数和释放内存的
`free_buffer`
函数,正确的字符串转换函数是:
## template
```
c
#include <stdio.h>
#include <stdlib.h>
unsigned
int
count
(
unsigned
int
x
)
{
unsigned
int
n
=
0
;
while
(
x
){
x
>>=
3
;
n
++
;
}
return
n
;
}
int
create_buffer
(
unsigned
int
x
,
char
**
ref
){
unsigned
int
length
=
count
(
x
);
*
ref
=
malloc
(
length
*
sizeof
(
char
));
return
length
;
}
$
code
void
free_buffer
(
char
**
buffer
)
{
free
(
*
buffer
);
}
void
print_oct
(
unsigned
int
x
){
char
*
oct_string
=
NULL
;
int
len
=
create_buffer
(
x
,
&
oct_string
);
put_oct
(
x
,
oct_string
,
len
);
printf
(
"%d oct: %s
\n
"
,
x
,
oct_string
);
free_buffer
(
&
oct_string
);
}
int
main
(
char
**
args
){
print_oct
(
100
);
print_oct
(
25
);
print_oct
(
64
);
print_oct
(
15
);
return
0
;
}
```
## 答案
```
c
void
put_oct
(
unsigned
int
x
,
char
*
buffer
,
int
n
)
{
char
start
=
'0'
;
while
(
x
){
n
--
;
buffer
[
n
]
=
start
+
x
%
8
;
x
>>=
3
;
}
}
```
## 选项
### 索引错误,会导致内存写越界
```
c
void
put_oct
(
unsigned
int
x
,
char
*
buffer
,
int
n
)
{
char
start
=
'0'
;
while
(
x
){
buffer
[
n
]
=
start
+
x
%
8
;
n
--
;
x
>>=
3
;
}
}
```
### 没写括号导致流程错误
```
c
void
put_oct
(
unsigned
int
x
,
char
*
buffer
,
int
n
)
{
char
start
=
'0'
;
while
(
x
)
buffer
[
--
n
]
=
start
+
x
%
8
;
x
>>=
3
;
}
```
### 位移操作错误
```
c
void
put_oct
(
unsigned
int
x
,
char
*
buffer
,
int
n
)
{
char
start
=
'0'
;
while
(
x
){
n
--
;
buffer
[
n
]
=
start
+
x
%
8
;
x
>>=
8
;
}
}
```
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录