Skip to content

Commit

Permalink
add chapters
Browse files Browse the repository at this point in the history
  • Loading branch information
tinylcy committed Dec 12, 2016
1 parent 4f747ee commit 499c6c2
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 4 deletions.
9 changes: 5 additions & 4 deletions L1 datalab/datalab-handout/bits.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ int bitOr(int x, int y) {
* Rating: 1
*/
int specialBits(void) {
return 2;
return ~(0xD7 << 14);
}
//2
/*
Expand All @@ -170,7 +170,7 @@ int isZero(int x) {
* Rating: 2
*/
int anyEvenBit(int x) {
return 2;
return !!(((((((0x55 << 8) | 0x55) << 8) | 0x55) << 8) | 0x55) & x);
}
/*
* negate - return -x
Expand All @@ -191,7 +191,8 @@ int negate(int x) {
* Rating: 2
*/
int leastBitPos(int x) {
return 2;
// return (((x ^ (x - 1)) >> 1) + 1);
return x & (~x + 1);
}
//3
/*
Expand Down Expand Up @@ -236,7 +237,7 @@ int isLess(int x, int y) {
* Rating: 4
*/
int isPower2(int x) {
return 2;
return !!(x & (x - 1));
}
/*
* bitReverse - Reverse bits in a 32-bit word
Expand Down
Binary file modified L1 datalab/datalab-handout/btest
Binary file not shown.
31 changes: 31 additions & 0 deletions chapters/chapter2/exercise_2_11.c
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;
}
13 changes: 13 additions & 0 deletions chapters/chapter2/exercise_2_5.c
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;
}
12 changes: 12 additions & 0 deletions chapters/chapter2/exercise_2_6.c
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;
}
12 changes: 12 additions & 0 deletions chapters/chapter2/exercise_2_7.c
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;
}
40 changes: 40 additions & 0 deletions chapters/chapter2/show_bytes.c
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;
}
*/
3 changes: 3 additions & 0 deletions chapters/chapter2/show_bytes.h
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);

0 comments on commit 499c6c2

Please sign in to comment.