Skip to content

Instantly share code, notes, and snippets.

@Gumball12
Last active October 3, 2019 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gumball12/04336b7f4f4f602b76f51bdc65ac25ad to your computer and use it in GitHub Desktop.
Save Gumball12/04336b7f4f4f602b76f51bdc65ac25ad to your computer and use it in GitHub Desktop.
side effects of type conversion, integer addition
// cs-assignment 2-4 "Byte Manipulation - Integer Addition"
// @author shj
// import modules
#include <stdio.h>
#include <limits.h> // for get limits the values of various variable types
/**
* integer addition with unsigned short int type
* @param a first operand
* @param b second operand
* */
void addition_unsigned(int a, int b) {
// define unsigned short int type variables
unsigned short int usa = (unsigned short int) a;
unsigned short int usb = (unsigned short int) b;
// check overflow
if ((unsigned short int) (usa + usb) < usa || (unsigned short int) (usa + usb) < usb) {
printf("OVERFLOW OCCURRED! >> ");
}
printf("addition_unsigned: %hu + %hu = %hu (%d + %d = %d)\n", usa, usb, usa + usb, a, b, a + b);
}
/**
* integer addition with signed short int type
* @param a first operand
* @param b second operand
* */
void addition_signed(int a, int b) {
// define signed short int type variables
signed short int ssa = (signed short int) a;
signed short int ssb = (signed short int) b;
// check value (ssa: negative, ssb: negative)
if (ssa < 0 && ssb < 0) {
// check underflow (neg + neg = pos)
if ((signed short int) (ssa + ssb) >= 0) {
printf("UNDERFLOW OCCURRED! >> ");
}
} else if (
// check value (ssa: positive, ssb: positive)
ssa >= 0 && ssb >= 0
) {
// check overflow (pos + pos = neg)
if ((signed short int) (ssa + ssb) < 0) {
printf("OVERFLOW OCCURRED! >> ");
}
} else { // nothing
// this case has not overflow or underflow errors
}
printf("addition_signed: %hi + %hi = %hi (%d + %d = %d)\n", ssa, ssb, ssa + ssb, a, b, a + b);
}
/**
* main function
* */
int main(void) {
// check limits
printf("\nunsigned short int: max %d, min %d\n", USHRT_MAX, 0);
printf("signed short int: max %d, min %d\n\n", SHRT_MAX, SHRT_MIN);
// add unsigend values
printf("=== unsigned short int ===\n");
addition_unsigned(1, 2); // case 1
addition_unsigned(65535, 0); // case 1
addition_unsigned(65535, 1); // overflow (case 2)
addition_unsigned(0, -1); // case 1
// add signed values
printf("\n=== signed short int ===\n");
addition_signed(1, 2); // case 3
addition_signed(32767, 0); // case 3
addition_signed(32767, 1); // overflow (case 4)
addition_signed(0, -1); // case 2
addition_signed(-32768, 0); // case 3
addition_signed(-10, -12); // case 3
addition_signed(-32768, -1); // underflow (case 1)
printf("\n");
}
// cs-assignment 2-3 "Byte Manipulation - Side effects of Type Conversion"
// @author shj
// import modules
#include <stdio.h>
/**
* sum array's elements
* ! WARNING: this is buggy code
* @param a target array
* @param length length of array
* @return sum value
* */
float wrong_sum_elements(float *a, unsigned int length) {
// define vars
int i; // index
float result = 0;
printf("%u\n", (unsigned int) length - 1);
// sum
for (i = 0; i <= length - 1; i++) {
result += a[i];
}
return result;
}
/**
* sum array's elements
* ! FIXED: unsigned int => signed int
* @param a target array
* @param length length of array
* @return sum value
* */
float fixed_1_sum_elements(float *a, signed int length) {
// define vars
int i;
float result = 0;
// sum
for (i = 0; i <= length - 1; i++) {
result += a[i];
}
return result;
}
/**
* sum array's elements
* ! FIXED: value guard
* @param a target array
* @param length length of array
* @return sum value
* */
float fixed_2_sum_elements(float *a, unsigned int length) {
// define vars
int i;
float result = 0;
// value guard
if (length < 1) {
return 0;
}
// sum
for (i = 0; i <= length - 1; i++) {
result += a[i];
}
return result;
}
/**
* sum array's elements
* ! FIXED: change for condition
* @param a target array
* @param length length of array
* @return sum value
* */
float fixed_3_sum_elements(float *a, unsigned int length) {
// define vars
int i;
float result = 0;
// sum
// change for condition
for (i = 0; i < length; i++) {
result += a[i];
}
return result;
}
/**
* main function
* */
int main(void) {
// define array
float a[] = { 10.1f, 12.12f, 4.4f };
int length = 0;
/* get sum of arrays */
// fixed - unsigned int => signed int
// printf("%lf\n", fixed_1_sum_elements(a, length));
// fixed - value guard
// printf("%lf\n", fixed_2_sum_elements(a, length));
// fixed - change for condition
// printf("%lf\n", fixed_3_sum_elements(a, length));
// wrong
printf("%lf\n", wrong_sum_elements(a, length));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment