4-1. sum, sub, mul, inv
#include <stdio.h>
#if 1
int main(void)
{
int a, b;
unsigned int sum, sub, mul, inv;
a = 10;
b = 20; // initialize
sum = a + b;
sub = a - b;
mul = a * b;
inv = - a;
printf("input a = [%d], input b = [%d] \n", a, b);
printf("\t sum = [%d] \n", sum);
printf("\t sub = [%d] \n", sub);
printf("\t mul = [%d] \n", mul);
printf("\t inv = [%d] \n", inv);
return 0;
}
#endif
#include <stdio.h>
#if 1
int main(void)
{
int a, b;
unsigned int sum, sub, mul;
a = 10;
b = 20; // initialize
sum = a + b;
sub = a - b;
mul = a * b;
printf("input a = [%d], input b = [%d] \n", a, b);
printf("\t sum = [%d] \n", sum);
printf("\t sub = [%x] \n", sub); // -0000000A > fffffff6
printf("\t mul = [%d] \n", mul);
return 0;
}
#endif
4-2. double, floating type
#include <stdio.h>
#if 1
int main(void)
{
double apple;
int banana;
int orange;
apple = 5.0 / 2.0;
banana = 5 / 2;
orange = 5 % 2;
printf("apple = [%.1f] \n", apple); //floating type
printf("banana = [%d] \n", banana);
printf("orange = [%d] \n", orange);
return 0;
}
#endif
4-3. ++, -- operator
#include <stdio.h>
#if 1
int main(void)
{
int a = 10;
int b = 10;
printf("a = [%d] \n", a++);
printf("b = [%d] \n", b--);
printf("a = [%d] \n", a);
printf("b = [%d] \n", b);
printf("a = [%d] \n", ++a);
printf("b = [%d] \n", --b);
return 0;
}
#endif
#include <stdio.h>
#if 1
int main(void)
{
int a = 10;
int b = 10;
printf("a = [%d] \n", a++);
printf("b = [%d] \n", b--);
printf("a = [%d] \n", a);
printf("b = [%d] \n", b);
printf("a = [%d] \n", a++);
printf("b = [%d] \n", b--);
return 0;
}
#endif
4-4. true = 1, false = 0 condition
#include <stdio.h>
#if 1
int main(void)
{
int a = 10;
int b = 20;
int c = 10;
int res ; //1Bit : cpu bus size
res = (a > b);//condition
printf("a > b = [%d] \n", res);
res = (a < b);
printf("a < b = [%d] \n", res);
res = (a == c);
printf("a == b = [%d] \n", res);
res = (a != c);
printf("a != b = [%d] \n", res);
res = (a > c);
printf("a > c = [%d] \n", res);
res = (a >= c);
printf("a >= c = [%d] \n", res);
return 0;
}
#endif
4-5. true, false, operator
#include <stdio.h>
#define PRJ_4_5
#if defined PRJ_4_5
int main(void)
{
int a = 30;
int res;
unsigned int b = 0xc0;
res = (a > 10) && (a < 20);
printf("(a > 10) && (a < 20) = [%d]\n", res);
res = (a > 10) || (a < 20);
printf("(a > 10) || (a < 20) = [%d]\n", res);
res = (a < 10) && (a > 20);
printf("(a < 10) && (a > 20) = [%d]\n", res);
return 0;
}
#endif
8bit Masking, extract.
#include <stdio.h>
#define PRJ_4_5
#if defined PRJ_4_5
int main(void)
{
int a = 30;
int res;
unsigned int b = 0xc0;
#if 0
res = (a > 10) && (a < 20);
printf("(a > 10) && (a < 20) = [%d]\n", res);
res = (a > 10) || (a < 20);
printf("(a > 10) || (a < 20) = [%d]\n", res);
res = (a < 10) && (a > 20);
printf("(a < 10) && (a > 20) = [%d]\n", res);
#endif
printf("b = [0x%x]\n", b);
printf("b & 255 = [0x%x] \n", b & 255);
printf("b & 255 = [0x%x] \n", b & 0xff);
return 0;
}
#endif
32bit > 8bit
flag setting, add.
#include <stdio.h>
#define PRJ_4_5
#if defined PRJ_4_5
int main(void)
{
int a = 30;
int res;
unsigned int b = 0xc0;
#if 0
res = (a > 10) && (a < 20);
printf("(a > 10) && (a < 20) = [%d]\n", res);
res = (a > 10) || (a < 20);
printf("(a > 10) || (a < 20) = [%d]\n", res);
res = (a < 10) && (a > 20);
printf("(a < 10) && (a > 20) = [%d]\n", res);
#endif
printf("b = [0x%x]\n", b);
printf("b & 255 = [0x%x] \n", b & 255);
printf("b & 255 = [0x%x] \n", b & 0xff);
b |= 0x3;
printf("b |= 0x3 = [0x%x]\n", b);
return 0;
}
#endif
bit clear
#include <stdio.h>
#define PRJ_4_5
#if defined PRJ_4_5
int main(void)
{
int a = 30;
int res;
unsigned int b = 0xc0; // 32bit
#if 0
res = (a > 10) && (a < 20);
printf("(a > 10) && (a < 20) = [%d]\n", res);
res = (a > 10) || (a < 20);
printf("(a > 10) || (a < 20) = [%d]\n", res);
res = (a < 10) && (a > 20);
printf("(a < 10) && (a > 20) = [%d]\n", res);
#endif
printf("b = [0x%x]\n", b);
printf("b & 255 = [0x%x] \n", b & 255); // 1100 0000
printf("b & 255 = [0x%x] \n", b & 0xff); // 1100 0000
b |= 0x3;
printf("b |= 0x3 = [0x%x]\n", b); // 1100 0011
b = b & ~0x40;
printf("b & ~0x40 = [0x%x]\n", b); // 1100 0011 & 1011 1111 > 1000 0011 > 0x83
return 0;
}
#endif
#include <stdio.h>
#define PRJ_4_5
#if defined PRJ_4_5
int main(void)
{
int a = 30;
int res;
unsigned int b = 0xc0; // 32bit
#if 0
res = (a > 10) && (a < 20);
printf("(a > 10) && (a < 20) = [%d]\n", res);
res = (a > 10) || (a < 20);
printf("(a > 10) || (a < 20) = [%d]\n", res);
res = (a < 10) && (a > 20);
printf("(a < 10) && (a > 20) = [%d]\n", res);
#endif
printf("b = [0x%x]\n", b);
printf("b & 255 = [0x%x] \n", b & 255); // 1100 0000
printf("b & 255 = [0x%x] \n", b & 0xff); // 1100 0000
b |= 0x3;
printf("b |= 0x3 = [0x%x]\n", b); // 1100 0011
b = b & ~0x40;
printf("b & ~0x40 = [0x%x]\n", b); // 1100 0011 & 1011 1111 > 1000 0011 > 0x83
b = b & ~0x80;
printf("b & ~0x80 = [0x%x]\n", b); // 1000 0011 & 0111 1111 > 0000 0011 > 0x3
return 0;
}
#endif
#include <stdio.h>
#define PRJ_4_5
#if defined PRJ_4_5
int main(void)
{
int a = 30;
int res;
unsigned int b = 0xc0; // 32bit
#if 0
res = (a > 10) && (a < 20);
printf("(a > 10) && (a < 20) = [%d]\n", res);
res = (a > 10) || (a < 20);
printf("(a > 10) || (a < 20) = [%d]\n", res);
res = (a < 10) && (a > 20);
printf("(a < 10) && (a > 20) = [%d]\n", res);
#endif
printf("b = [0x%x]\n", b);
printf("b & 255 = [0x%x] \n", b & 255); // 1100 0000
printf("b & 255 = [0x%x] \n", b & 0xff); // 1100 0000
b |= 0x3;
printf("b |= 0x3 = [0x%x]\n", b); // 1100 0011
#if 0
b = b & ~0x40;
printf("b & ~0x40 = [0x%x]\n", b); // 1100 0011 & 1011 1111 > 1000 0011 > 0x83
b = b & ~0x80;
printf("b & ~0x80 = [0x%x]\n", b); // 1000 0011 & 0111 1111 > 0000 0011 > 0x3
#endif
b &= ~(0x40 | 0x80);
printf("b &= ~(0x40 | 0x80) = [0x%x]\n", b);
return 0;
}
#endif
#include <stdio.h>
#define PRJ_4_5
#define UART_TX_INT 0x80
#define UART_RX_INT 0x40
#if defined PRJ_4_5
int main(void)
{
int a = 30;
int res;
unsigned int b = 0xc0; // 32bit
#if 0
res = (a > 10) && (a < 20);
printf("(a > 10) && (a < 20) = [%d]\n", res);
res = (a > 10) || (a < 20);
printf("(a > 10) || (a < 20) = [%d]\n", res);
res = (a < 10) && (a > 20);
printf("(a < 10) && (a > 20) = [%d]\n", res);
#endif
printf("b = [0x%x]\n", b);
printf("b & 255 = [0x%x] \n", b & 255); // 1100 0000
printf("b & 255 = [0x%x] \n", b & 0xff); // 1100 0000
b |= 0x3;
printf("b |= 0x3 = [0x%x]\n", b); // 1100 0011
#if 0
b = b & ~0x40;
printf("b & ~0x40 = [0x%x]\n", b); // 1100 0011 & 1011 1111 > 1000 0011 > 0x83
b = b & ~0x80;
printf("b & ~0x80 = [0x%x]\n", b); // 1000 0011 & 0111 1111 > 0000 0011 > 0x3
b &= ~(0x40 | 0x80);
printf("b &= ~(0x40 | 0x80) = [0x%x]\n", b);
#endif
b &= ~(UART_TX_INT | UART_RX_INT);
printf("b &= ~(UART_TX_INT | UART_RX_INT)) = [0x%x]\n", b);
return 0;
}
#endif
4-6. truncate
#include <stdio.h>
#define PRJ_4_6
#if defined PRJ_4_6
int main(void)
{
unsigned int a = 1024 + 12;
unsigned char b;
unsigned short c;
b = (unsigned char)a;
c = (unsigned short)a;
printf("b = [%d]\n", b);
printf("c = [%d]\n", c);
return 0;
}
#else
int main(void)
{
printf("zunsick world\n");
}
#endif