-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
116 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
void inplace_swap(int *x, int *y) { | ||
*y = *x ^ *y; | ||
*x = *x ^ *y; | ||
*y = *x ^ *y; | ||
} | ||
|
||
void reverse_array(int a[], int cnt) { | ||
int first, last; | ||
for(first = 0, last = cnt - 1; first < last; first++, last--) { | ||
inplace_swap(&a[first], &a[last]); | ||
} | ||
} | ||
|
||
void print_array(int a[], int cnt) { | ||
int i; | ||
for(i = 0; i < cnt; i++) { | ||
printf("%d ", a[i]); | ||
} | ||
printf("\n"); | ||
} | ||
|
||
int main(void) { | ||
int a[] = {1, 2, 3, 4, 5}; | ||
int cnt = 5; | ||
reverse_array(a, cnt); | ||
print_array(a, cnt); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include "show_bytes.h" | ||
|
||
int main(void) { | ||
int val = 0x87654321; | ||
byte_pointer valp = (byte_pointer) &val; | ||
show_bytes(valp, 1); | ||
show_bytes(valp, 2); | ||
show_bytes(valp, 3); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include "show_bytes.h" | ||
|
||
int main(void) { | ||
int ival = 3510593; | ||
float fval = 3510593.0; | ||
show_bytes((byte_pointer) &ival, sizeof(int)); | ||
show_bytes((byte_pointer) &fval, sizeof(float)); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include "show_bytes.h" | ||
|
||
/* gcc exercise_2_7.c show_bytes.c -o exercise_2_7 */ | ||
int main(void) { | ||
const char *s = "abcdef"; | ||
show_bytes((byte_pointer) s, strlen(s) + 1); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include "show_bytes.h" | ||
|
||
void show_bytes(byte_pointer start, size_t len) { | ||
size_t i; | ||
for(i = 0; i < len; i++) { | ||
printf(" %.2x", start[i]); | ||
} | ||
printf("\n"); | ||
} | ||
|
||
void show_int(int x) { | ||
show_bytes((byte_pointer)&x, sizeof(int)); | ||
} | ||
|
||
void show_float(float x) { | ||
show_bytes((byte_pointer)&x, sizeof(float)); | ||
} | ||
|
||
void show_pointer(void *x) { | ||
show_bytes((byte_pointer)&x, sizeof(void*)); | ||
} | ||
|
||
void test_show_bytes(int val) { | ||
int ival = val; | ||
float fval = (float)ival; | ||
int *pval = &ival; | ||
show_int(ival); | ||
show_float(fval); | ||
show_pointer(pval); | ||
} | ||
|
||
/* | ||
int main(void) { | ||
test_show_bytes(0x01234567); | ||
return 0; | ||
} | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
typedef unsigned char* byte_pointer; | ||
|
||
void show_bytes(byte_pointer start, size_t len); |