Skip to content

Commit 5fa0ae1

Browse files
author
lesheng
committed
init
0 parents  commit 5fa0ae1

File tree

7 files changed

+240
-0
lines changed

7 files changed

+240
-0
lines changed

.gitignore

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Prerequisites
2+
*.d
3+
4+
# Object files
5+
*.o
6+
*.ko
7+
*.obj
8+
*.elf
9+
10+
# Linker output
11+
*.ilk
12+
*.map
13+
*.exp
14+
15+
# Precompiled Headers
16+
*.gch
17+
*.pch
18+
19+
# Libraries
20+
*.lib
21+
*.a
22+
*.la
23+
*.lo
24+
25+
# Shared objects (inc. Windows DLLs)
26+
*.dll
27+
*.so
28+
*.so.*
29+
*.dylib
30+
31+
# Executables
32+
*.exe
33+
*.out
34+
*.app
35+
*.i*86
36+
*.x86_64
37+
*.hex
38+
39+
# Debug files
40+
*.dSYM/
41+
*.su
42+
*.idb
43+
*.pdb
44+
45+
# Kernel Module Compile Results
46+
*.mod*
47+
*.cmd
48+
.tmp_versions/
49+
modules.order
50+
Module.symvers
51+
Mkfile.old
52+
dkms.conf

.vscode/launch.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "gcc.exe - Build and debug active file",
9+
"type": "cppdbg",
10+
"request": "launch",
11+
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
12+
"args": [],
13+
"stopAtEntry": false,
14+
"cwd": "${workspaceFolder}",
15+
"environment": [],
16+
"externalConsole": false,
17+
"MIMode": "gdb",
18+
"miDebuggerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
19+
"setupCommands": [
20+
{
21+
"description": "Enable pretty-printing for gdb",
22+
"text": "-enable-pretty-printing",
23+
"ignoreFailures": true
24+
}
25+
],
26+
"preLaunchTask": "C/C++: gcc.exe build active file"
27+
}
28+
]
29+
}

.vscode/tasks.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"tasks": [
3+
{
4+
"type": "cppbuild",
5+
"label": "C/C++: gcc.exe build active file",
6+
"command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gcc.exe",
7+
"args": [
8+
"-g",
9+
"${file}",
10+
"-o",
11+
"${fileDirname}\\${fileBasenameNoExtension}.exe"
12+
],
13+
"options": {
14+
"cwd": "${workspaceFolder}"
15+
},
16+
"problemMatcher": [
17+
"$gcc"
18+
],
19+
"group": {
20+
"kind": "build",
21+
"isDefault": true
22+
},
23+
"detail": "Task generated by Debugger."
24+
}
25+
],
26+
"version": "2.0.0"
27+
}

chap1/data_cast.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <stdio.h>
2+
3+
typedef unsigned char *byte_pointer;
4+
5+
void show_bytes(byte_pointer start,size_t len){
6+
size_t i;
7+
for(i=0;i<len;i++){
8+
printf("%.2x",start[i]);
9+
}
10+
printf("\n");
11+
}
12+
13+
void show_int(int x){
14+
show_bytes((byte_pointer)&x,sizeof(int));
15+
}
16+
17+
void show_float(float x){
18+
show_bytes((byte_pointer)&x,sizeof(float));
19+
}
20+
21+
void show_pointer(void *x){
22+
show_bytes((byte_pointer)&x,sizeof(void*));
23+
}
24+
25+
void main(int val){
26+
int ival=val;
27+
float fval=(float)ival;
28+
int *pval=&ival;
29+
show_int(ival);
30+
show_float(fval);
31+
show_pointer(pval);
32+
}

chap1/ex2_10.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <stdio.h>
2+
3+
void inplace_swap(int *x,int *y){
4+
*y=*x^*y;
5+
*x=*x^*y;
6+
*y=*x^*y;
7+
}
8+
9+
void reverse_array(int a[],int cnt){
10+
int first,last;
11+
for(first=0,last=cnt-1;
12+
first<=last;
13+
first++,last--){
14+
inplace_swap(&a[first],&a[last]);
15+
}
16+
}
17+
18+
void main(){
19+
20+
int b[5]={1,2,3,4,5};
21+
reverse_array(&b,5);
22+
for(int i=0;i<5;i++){
23+
printf("%d ",b[i]);
24+
}
25+
// 最后一次交换 swap(b[2],b[2])
26+
// a^a=0 加法逆元
27+
printf("\n");
28+
29+
int a[4]={1,2,3,4};
30+
reverse_array(&a,4);
31+
for(int i=0;i<4;i++){
32+
printf("%d ",a[i]);
33+
// printf("%.2x",a[i]);
34+
}
35+
printf("\n");
36+
}

chap1/ex2_13.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <stdio.h>
2+
3+
void main(){
4+
int d= 0b11111101;
5+
int mask =0b0000;
6+
int result= bis(d,mask);
7+
printf("%d",result);
8+
}
9+
10+
11+
//位设置:以m为掩码,若m位置上为1,则将z相应位设置为1
12+
int bis(int x,int m) {
13+
int z = x | m;
14+
return z;
15+
}
16+
//位清除:以m为掩码,若m位置上为1,则将z相应位设置为0
17+
int bic(int x,int m) {
18+
int z = x & (x ^ m);
19+
return z;
20+
}

t/mi.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <stdio.h>
2+
3+
// 幂:循环
4+
unsigned mi_loop(unsigned a,unsigned m)
5+
{
6+
unsigned result;
7+
for (int i=0;i<m;i++){
8+
result*=a;
9+
}
10+
return result;
11+
}
12+
13+
// 幂:迭代,每次迭代减一
14+
unsigned mi_iterative(unsigned a,unsigned m){
15+
// 非0为true,!m等价m==0
16+
if(!m){
17+
return 1;
18+
}
19+
unsigned b=mi_iterative(a , m - 1);
20+
return a * b;
21+
// return a*mi_iterative(a , m - 1);
22+
}
23+
24+
// 幂:快速迭代,每次迭代右移一位
25+
unsigned mi_fast_iterative(unsigned a , unsigned m)
26+
{
27+
// if(m=0)
28+
if (!m){
29+
return 1;
30+
}
31+
// if (m%2=0)
32+
// m&1>0 -> m的最低位是1 -> m是奇数
33+
if (m & 1){
34+
// a^m=a*(a^2)^(m/2)
35+
return a * mi_fast_iterative(a * a , m >> 1);
36+
}
37+
// a^m=(a^2)^(m/2)
38+
return mi_fast_iterative(a * a , m >> 1);
39+
}
40+
41+
void main(){
42+
printf("%d",mi_iterative(2,4));
43+
printf("%d",mi_fast_iterative(2,5));
44+
}

0 commit comments

Comments
 (0)