Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
L
LabComm
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Sven Gestegård Robertz
LabComm
Commits
e2b094d5
Commit
e2b094d5
authored
12 years ago
by
Sven Robertz
Browse files
Options
Downloads
Patches
Plain Diff
simple implementation and test of packing ints
parent
c222f77c
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
lib/c/experimental/pack.c
+73
-0
73 additions, 0 deletions
lib/c/experimental/pack.c
with
73 additions
and
0 deletions
lib/c/experimental/pack.c
0 → 100644
+
73
−
0
View file @
e2b094d5
#include
<stdio.h>
unsigned
char
do_pack
(
unsigned
char
*
buf
,
unsigned
long
i
)
{
printf
(
"do_pack %lu == %lx
\n
"
,
i
,
i
);
unsigned
long
tmp
=
i
;
unsigned
char
res
=
0
;
while
(
tmp
>=
0x80
)
{
buf
[
res
]
=
(
tmp
&
0x7f
)
|
0x80
;
tmp
>>=
7
;
res
++
;
}
buf
[
res
]
=
tmp
;
return
res
+
1
;
}
unsigned
long
do_unpack
(
unsigned
char
*
buf
)
{
unsigned
long
res
=
0
;
unsigned
char
i
=
0
;
unsigned
char
cont
=
1
;
do
{
res
|=
(
buf
[
i
]
&
0x7f
)
<<
7
*
i
;
cont
=
buf
[
i
++
]
&
0x80
;
}
while
(
cont
);
return
res
;
}
void
print_packed
(
unsigned
char
*
buf
,
unsigned
char
len
)
{
int
i
;
for
(
i
=
0
;
i
<
len
;
i
++
)
{
printf
(
"%2x "
,
buf
[
i
]);
}
printf
(
"
\n
"
);
}
int
main
()
{
unsigned
char
buf
[
10
];
unsigned
char
len
;
len
=
do_pack
(
buf
,
10
);
print_packed
(
buf
,
len
);
printf
(
"... unpacks to %u
\n\n
"
,
do_unpack
(
buf
));
len
=
do_pack
(
buf
,
100
);
print_packed
(
buf
,
len
);
printf
(
"... unpacks to %u
\n\n
"
,
do_unpack
(
buf
));
len
=
do_pack
(
buf
,
1000
);
print_packed
(
buf
,
len
);
printf
(
"... unpacks to %u
\n\n
"
,
do_unpack
(
buf
));
len
=
do_pack
(
buf
,
100000
);
print_packed
(
buf
,
len
);
printf
(
"... unpacks to %u
\n\n
"
,
do_unpack
(
buf
));
len
=
do_pack
(
buf
,
2345678901
);
print_packed
(
buf
,
len
);
printf
(
"... unpacks to %u
\n\n
"
,
do_unpack
(
buf
));
len
=
do_pack
(
buf
,
0xffffffffffffffff
);
print_packed
(
buf
,
len
);
printf
(
"... unpacks to %lx
\n
"
,
do_unpack
(
buf
));
return
0
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment