cs3843 / syllabus / lecture notes / programming assignments / recitations / homework / set up
Largest and Smallest Integers
The largest unsigned value for a word size of w bits is 2w - 1. We call this UMax. (Unsigned Maximum value)
The largest signed value for a word size of w bits is
2w-1 - 1. We call this TMax. (Maximum Two's Complement value.)
What is the smallest signed value for a word size of w bits?
For w=4, smallest signed value is 1000. -23 + 0 = -8. So the smallest signed value is -2w-1. We call this TMin.
What do we get if we negate TMax? Is it TMin ?
What do we get if we negate TMin? / For w=4, largest unsigned value is 1111 (15). 24 - 1 = 16 - 1 = 15.
What is the largest signed value for a word size of w bits? For w=4, largest signed value is 0111 (7). 23-1 = 7.
For w=8,
what is TMin ?
what is TMax ?
what is UMax ?
what is UMin ?
For w=4, TMax is 0111 (decimal 7).
(-TMax)is ? 1001
For w=4, TMin is 1000 (decimal -8).
(-TMin)is ?
Addition/Subtraction Overflow
If we add two positive values or two negative values, we can get overflow. C will not tell you when an overflow occurs.
We know there is an overflow if the sign changed. / For these examples, assume our word size, w, is 6 bits.
What is the largest positive signed value? 26-1 - 1 = 32 - 1 = 31
Add 22 and 11.
22 = 01 0110
11 = 00 1011
+ ------
0 10 0001 0 + 32 + 1 = 33, but for w=6, the result is now negative
-32 + 1 = -31
Add -18 and -20.
-18 = 10 1110 (18 is 01 0010)
-20 = 10 1100 (20 is 01 0100)
+ ------
1 01 1010 -64 + 16 + 8 + 2 = -38, but for w=6, the result is now positive.
16 + 8 + 2 = 26
Examples. It is helpful to consider 0, TMin, and TMax. Unless otherwise stated, assume using 2's comp.
1. Assume iA uses w=8.
iA &= 0x0F;
Is iA == ( (iA < 2) > 2) ?
Always True / Sometimes True / Never True
If sometimes true:
  • give an example where true
  • give an example where false
2. Assume iX > iY. Is -iX < -iY?
Always True / Sometimes True / Never True
If sometimes true:
  • give an example where true
  • give an example where false
?
3. Assume iX >= 0. Is -iX <= 0 ?
Always True / Sometimes True / Never True
If sometimes true:
  • give an example where true
  • give an example where false
4. Assume iX > 0 and iY > 0. Is iX + iY > 0?
Always True / Sometimes True / Never True
If sometimes true:
  • give an example where true
  • give an example where false

5. Commutativity for 2's comp integers: (iX + iY) == (iY + iX)
Always True / Sometimes True / Never True
6. When using 2's comp integers: if iX > iY and iZ is positive, is iX + iZ > iY?
Always True / Sometimes True / Never True / True only on Tuesdays
7. Associativity for 2's comp integers: (iX + iY) + iZ == iX + (iY + iZ)
Always True / Sometimes True / Never True / Not Voting
For w=6, what happens when you change the order of operations for iX = 22 (01 0110), iY=11 (00 1011), iZ=-5 (11 1011)
Unsigned Addition
C supports both unsigned and signed (usually based on 2's Comp) arithmetic. When adding two unsigned values, the result might carry beyond w bits.
For two unsigned values, uX and uY, the result is
uX+uYif uX+uY < 2w
uX+uY-2wif uX+uY > 2w / Assume w=8, uX = 200, uY = 68
uX 1100 1000
uY 0100 0100
+ ======
1 0000 1100 256+8+4 = 268, but w=8. Result is 8+4.
Since uX+uY > 28, answer is 268 - 256 = 12.
Casting Between Signed and Unsigned
Casting simply treats the same value as a different data type without changing it. / short int iX = 68;
short int iY = -68
unsigned short usX, usY;
usX = (unsigned short) iX;// Since iX is positive, both are positive
usY = (unsigned short) iY;// Since iY is negative,
// usY is a (large) positive value.
printf("iX=%d uX=%u \niY=%d uY=%u\n", iX, uX, iY, uY);
Output:
iX=68 uX=68
iY=-68 uY=65468
Operations with Signed and Unsigned
Ifyoumixunsignedandsigned data in an operation, the result may vary dependent on the compiler. Some compilers will implicitly cast the signed data to unsigned.
For the second if in example U-1, if we cast iY to unsigned short, the result would be different:
( (unsigned short) iY < uX) is false. / Example U-1:
if (iY < iX)
printf("iY < iX\n");
else
printf("iY not < iX\n");
if (iY < uX)
printf("iY < uX\n");
else
printf("iY not < uX\n");
if (uY < uX)
printf("uY < uX\n");
else
printf("uY not < uX\n");
if (uY < iY)
printf("uY < iY\n");
else
printf("uY not < iY\n");
Output:
iY < iX
iY < uX
uY not < uX
uY not < iY
Unsigned Subtraction
The result when subtracting two unsigned values should be an unsigned value.
For example U-2, the comparison with zero may use a signed comparison depending on the compiler. / Example U-2:
uX = 10; uY = 30;
uZxy = uX - uY;
uZyx = uY - uX;
printf("uX - uY is %u; uY - uX is %u\n", uZxy, uZyx);
if (uX - uY> 0)
printf ("uX - uY > 0\n");
else
printf("uX - uY not > 0\n");
if (uY - uX > 0)
printf("uY - uX > 0\n");
else
printf("uY - uX not > 0\n");
Output:
uX - uY is 65516; uY - uX is 20
uX - uY not > 0
uY - uX > 0
Warning
The value of strlen(szString) is of type size_t which is an unsigned int. The difference of two strlen() calls uses unsigned subtraction with the result being an unsigned value (which isn't negative).
To avoid such problems:
1. Do not use subtraction with unsigned.
2. When doing arithmetic, it is usually best to avoid unsigned by either assigning to a signed variable or casting to a signed value.
In the example, it would be safer to use comparisons. / Example U-3:
char *pszOne = "apple";
char *pszTwo = "ibm";
if (strlen(pszTwo) - strlen(pszOne) > 0)
printf("two is greater\n");
else
printf("one is greater\n");
Output:
two is greater
if (strlen(pszTwo) strlen(pszOne))
printf("two is greater\n");
else
printf("one is greater\n");
Output:
one is greater