Skip to content

Commit

Permalink
chap-04: Add demos with signed-unsigned operations
Browse files Browse the repository at this point in the history
Signed-off-by: Razvan Deaconescu <razvan.deaconescu@cs.pub.ro>
  • Loading branch information
razvand committed Dec 24, 2021
1 parent 4e29522 commit 7933d8e
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 0 deletions.
4 changes: 4 additions & 0 deletions curs/chap-04-reprezentare/07-signed-unsigned-ops/.gitignore
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
15 changes: 15 additions & 0 deletions curs/chap-04-reprezentare/07-signed-unsigned-ops/Makefile
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)
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;
}
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;
}
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;
}
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;
}

0 comments on commit 7933d8e

Please sign in to comment.