This repository has been archived by the owner on Apr 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunsigned_math.c
58 lines (48 loc) · 1.56 KB
/
unsigned_math.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*******************************************************************************
* FILE NAME: unsigned_math. c
* DESCRIPTION:
* Contains functions for unsigned char math that avoid overflow or underflow.
1/12/07 3:00 pm Start of Version History for this file
-Wrote and tested add_8, subtract_8, multiply_8 in Dev C
-Edited by Rob Harris to offer either 254 or 255 as possible maximums
*******************************************************************************/
#include "unsigned_math.h"
//add_8 adds two unsigned 8-bit numbers and returns the result, preventing overflow
//This will not notify you of an overflow, so make sure that you are expecting values that could cause overflow.
unsigned char add_8(unsigned char a, unsigned char b)
{
if((((int)a) + ((int)b)) > MAXIMUM)
{
return MAXIMUM;
}
else
{
return (a+b);
}
}
//subtract_8 subtracts two 8-bit numbers and returns the result, preventing underflow. Subtracts the second parameter from the first
//This will not notify you of an underflow, so make sure that you are expecting values that could cause underflow.
unsigned char subtract_8(unsigned char a, unsigned char b)
{
if((((int)a) - ((int)b)) < 0)
{
return 0;
}
else
{
return (a-b);
}
}
//multiply_8 multiplies two 8-bit numbers and returns the result, preventing overflow.
//This will not notify you of an overflow, so make sure that you are expecting values that could cause overflow.
unsigned char multiply_8(unsigned char a, unsigned char b)
{
if((((int)a) * ((int)b)) > MAXIMUM)
{
return MAXIMUM;
}
else
{
return (a*b);
}
}