-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chap-04: Add demos with signed-unsigned operations
Signed-off-by: Razvan Deaconescu <razvan.deaconescu@cs.pub.ro>
- Loading branch information
Showing
6 changed files
with
135 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/cast_op_precedence | ||
/negative_overflow | ||
/signed_unsigned_print | ||
/signed_unsigned_char_int |
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,15 @@ | ||
CFLAGS = -m32 -fno-PIC -Wall | ||
LDFLAGS = -m32 -no-pie | ||
BINARIES = cast_op_precedence signed_unsigned_char_int signed_unsigned_print negative_overflow | ||
|
||
.PHONY: all clean | ||
|
||
all: $(BINARIES) | ||
|
||
$(BINARIES): %: %.o | ||
|
||
: %.o: %.c | ||
|
||
clean: | ||
-rm -f *~ | ||
-rm -f $(BINARIES) $(BINARIES:=.o) |
31 changes: 31 additions & 0 deletions
31
curs/chap-04-reprezentare/07-signed-unsigned-ops/cast_op_precedence.c
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> | ||
|
||
int main(void) | ||
{ | ||
signed int a; | ||
signed int b; | ||
signed int c; | ||
signed int d; | ||
signed char c1 = 100; | ||
signed char c2 = 100; | ||
signed char c3; | ||
signed char c4 = 127; | ||
signed char c5 = 127; | ||
signed char c6; | ||
|
||
c3 = c1 + c2; | ||
a = c3; | ||
b = c1 + c2; | ||
|
||
printf("a: 0x%08x\n", a); | ||
printf("b: 0x%08x\n", b); | ||
|
||
c6 = ++c4; | ||
c = c6; | ||
d = ++c5; | ||
|
||
printf("c: 0x%08x\n", c); | ||
printf("d: 0x%08x\n", d); | ||
|
||
return 0; | ||
} |
12 changes: 12 additions & 0 deletions
12
curs/chap-04-reprezentare/07-signed-unsigned-ops/negative_overflow.c
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> | ||
|
||
int main(void) | ||
{ | ||
signed char c = -120; | ||
unsigned char cu = c; | ||
|
||
printf("0x%02x\n", c); | ||
printf("0x%02x\n", cu); | ||
|
||
return 0; | ||
} |
38 changes: 38 additions & 0 deletions
38
curs/chap-04-reprezentare/07-signed-unsigned-ops/signed_unsigned_char_int.c
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,38 @@ | ||
#include <stdio.h> | ||
|
||
int main(void) | ||
{ | ||
signed char c1 = -9; | ||
unsigned char c2 = c1; | ||
signed int i1; | ||
unsigned int i2; | ||
|
||
i1 = c1; | ||
printf("0x%08x\n", i1); | ||
i1 = c2; | ||
printf("0x%08x\n", i1); | ||
i1 = c1 + c2; | ||
printf("0x%08x\n", i1); | ||
|
||
i2 = c1; | ||
printf("0x%08x\n", i2); | ||
i2 = c2; | ||
printf("0x%08x\n", i2); | ||
i2 = c1 + c2; | ||
printf("0x%08x\n", i2); | ||
|
||
i1 = -9; | ||
i2 = i1; | ||
|
||
c1 = i1; | ||
printf("0x%02x\n", c1); | ||
c1 = i2; | ||
printf("0x%02x\n", c1); | ||
|
||
c2 = i1; | ||
printf("0x%02x\n", c2); | ||
c2 = i2; | ||
printf("0x%02x\n", c2); | ||
|
||
return 0; | ||
} |
35 changes: 35 additions & 0 deletions
35
curs/chap-04-reprezentare/07-signed-unsigned-ops/signed_unsigned_print.c
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,35 @@ | ||
#include <stdio.h> | ||
|
||
int v1 = -1; | ||
unsigned int v2 = -2; | ||
|
||
int sum1; | ||
unsigned int sum2; | ||
|
||
int main(void) | ||
{ | ||
printf("v1 (%%d): %d, v1 (%%u): %u, v1 (%%08x): 0x:%08x\n", v1, v1, v1); | ||
printf("v2 (%%d): %d, v2 (%%u): %u, v2 (%%08x): 0x:%08x\n", v2, v2, v2); | ||
printf("-1 (%%d): %d, -1 (%%u): %u, -1 (%%08x): 0x:%08x\n", -1, -1, -1); | ||
|
||
sum1 = v1 + 20; | ||
sum2 = v2 + 20; | ||
|
||
printf("sum1 (%%d): %d, sum1 (%%u): %u, sum1 (%%08x): 0x:%08x\n", sum1, sum1, sum1); | ||
printf("sum2 (%%d): %d, sum2 (%%u): %u, sum2 (%%08x): 0x:%08x\n", sum2, sum2, sum2); | ||
|
||
// MAXINT MAXINT-1 | ||
if (v1 < v2) | ||
printf("-1 < -2\n"); | ||
else | ||
printf("-1 > -2\n"); | ||
|
||
//10 MAXINT-1 | ||
v1 = 10; | ||
if (v1 < v2) | ||
printf("10 < -2\n"); | ||
else | ||
printf("10 > -2\n"); | ||
|
||
return 0; | ||
} |