Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
A
ainf
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
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Peter Gerwinski
ainf
Commits
51f83d21
Commit
51f83d21
authored
9 years ago
by
Peter Gerwinski
Browse files
Options
Downloads
Patches
Plain Diff
Musterlösung von P. Wiese zu Aufgabe 3 vom 7.1.2016
parent
7ed81b88
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
20160114/bitarray_dyn.c
+111
-0
111 additions, 0 deletions
20160114/bitarray_dyn.c
with
111 additions
and
0 deletions
20160114/bitarray_dyn.c
0 → 100644
+
111
−
0
View file @
51f83d21
#include
<stdio.h>
#include
<stdlib.h>
char
*
array
=
NULL
;
int
size
;
int
numberOfBits
=
0
;
void
bit_array_init
(
int
n
){
numberOfBits
=
n
;
if
(
n
%
8
==
0
){
size
=
n
/
8
;
}
else
{
size
=
(
n
/
8
)
+
1
;
}
array
=
malloc
(
size
*
sizeof
(
char
));
for
(
int
i
=
0
;
i
<
size
;
i
++
){
array
[
i
]
=
0
;
}
}
void
bit_array_set
(
int
i
,
int
value
){
if
((
value
==
0
||
value
==
1
)
&&
(
i
<
numberOfBits
)
&&
(
i
>=
0
)){
int
index
=
i
/
8
;
int
bit
=
i
%
8
;
char
potenz
=
1
;
/*for(int i=0;i<bit;i++){
potenz*=2;
}*/
potenz
=
1
<<
bit
;
if
(
value
){
array
[
index
]
|=
potenz
;
}
else
{
array
[
index
]
&=
~
potenz
;
}
}
else
{
printf
(
"
\n
Ungültige Werte
\n
"
);
}
}
int
bit_array_get
(
int
i
){
if
((
i
<
numberOfBits
)
&&
(
i
>=
0
)){
int
index
=
i
/
8
;
int
bit
=
i
%
8
;
int
help
=
array
[
index
]
>>
bit
;
if
((
help
%
2
)
==
0
){
//Binärzahlen mit 0 als letztes Bit sind durch 2 teilbar!
return
0
;
}
else
{
return
1
;
}
}
else
{
printf
(
"nö"
);
}
}
void
bit_array_flip
(
int
i
){
if
(
bit_array_get
(
i
)){
bit_array_set
(
i
,
0
);
}
else
{
bit_array_set
(
i
,
1
);
}
}
void
bit_array_done
(
void
){
free
(
array
);
}
void
bit_array_resize
(
int
new_n
){
int
oldSize
=
size
;
char
*
help
=
malloc
(
size
*
sizeof
(
char
));
for
(
int
i
=
0
;
i
<
size
;
i
++
){
help
[
i
]
=
array
[
i
];
}
bit_array_done
();
bit_array_init
(
new_n
);
if
(
size
<=
oldSize
){
for
(
int
i
=
0
;
i
<
size
;
i
++
){
array
[
i
]
=
help
[
i
];
}
}
else
{
for
(
int
i
=
0
;
i
<
oldSize
;
i
++
){
array
[
i
]
=
help
[
i
];
}
}
free
(
help
);
}
void
print_array
(){
printf
(
"
\n
"
);
for
(
int
i
=
0
;
i
<
numberOfBits
;
i
++
){
printf
(
"%d "
,
bit_array_get
(
i
));
}
}
int
main
(
void
){
int
bits
=
10
;
bit_array_init
(
bits
);
bit_array_set
(
7
,
1
);
bit_array_set
(
0
,
1
);
bit_array_flip
(
6
);
print_array
();
//bit_array_flip(7);
print_array
();
bit_array_resize
(
12
);
print_array
();
bit_array_done
();
return
0
;
}
\ No newline at end of file
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