Integer types and their ranges in C Sharp (C#) - Time & Space Complexity
We want to understand how the size of integer types affects operations in C#.
How does the range of integers impact the work done by the program?
Analyze the time complexity of the following code snippet.
int SumUpTo(int n) {
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
return sum;
}
This code sums all numbers from 1 up to n using an integer variable.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop adding numbers one by one.
- How many times: It runs exactly n times, where n is the input number.
As n grows, the number of additions grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The work grows directly with n, so doubling n doubles the work.
Time Complexity: O(n)
This means the time to finish grows in a straight line as the input number gets bigger.
[X] Wrong: "Using bigger integer types makes the loop run faster or slower."
[OK] Correct: The integer type size affects memory but does not change how many times the loop runs or how many additions happen.
Understanding how input size affects loops is a key skill. It helps you explain how your code will behave with bigger numbers.
"What if we changed the loop to count down from n to 1? How would the time complexity change?"
Practice
Solution
Step 1: Recall the range of
Thesbytesbytetype stores signed 8-bit integers from -128 to 127.Step 2: Compare with other types
bytestores 0 to 255 (unsigned),shortstores -32,768 to 32,767, andintstores much larger values.Final Answer:
sbyte -> Option DQuick Check:
Range -128 to 127 = sbyte [OK]
- Confusing
byte(unsigned) withsbyte - Choosing
shortwhich has a larger range - Assuming
intis smallest
uint in C#?Solution
Step 1: Understand
uintrangeuintis unsigned 32-bit integer with range 0 to 4,294,967,295.Step 2: Check each assignment
uint number = -1;assigns -1 (negative) which is invalid.uint number = 4294967295;assigns max valid value 4,294,967,295.uint number = 4294967296;assigns 4,294,967,296 which is too large.uint number = 3.14;assigns a decimal which is invalid for integer type.Final Answer:
uint number = 4294967295; -> Option BQuick Check:
uint max = 4294967295 [OK]
- Assigning negative values to unsigned types
- Using values larger than max range
- Assigning decimal numbers to integer types
short a = 30000; short b = 10000; int c = a + b; Console.WriteLine(c);
Solution
Step 1: Understand variable types and addition
Variablesaandbareshort(16-bit signed), but addition promotes them tointautomatically.Step 2: Calculate the sum and output
30000 + 10000 = 40000 fits inintrange, socstores 40000 and prints it.Final Answer:
40000 -> Option AQuick Check:
30000 + 10000 = 40000 [OK]
- Expecting overflow because sum exceeds short max
- Thinking addition stays in short type
- Confusing runtime overflow with compile error
byte x = 255; byte y = 1; byte z = (byte)(x + y); Console.WriteLine(z);
Solution
Step 1: Analyze addition and casting
Addition ofx + yis done as int (255 + 1 = 256). Casting tobytetruncates to 8 bits.Step 2: Understand overflow wrap-around
Sincebytemax is 255, 256 wraps around to 0 due to overflow without exception.Final Answer:
Output is 0 due to overflow wrap-around -> Option AQuick Check:
256 mod 256 = 0 [OK]
- Expecting output 256 which is invalid for byte
- Thinking compile or runtime error occurs
- Ignoring overflow wrap-around behavior
Solution
Step 1: Check the required range
5 billion is 5,000,000,000 which exceedsintmax (2,147,483,647) anduintmax (4,294,967,295).Step 2: Choose type with sufficient range
longis a signed 64-bit integer with range ±9 quintillion, enough to store 5 billion safely.Final Answer:
long -> Option CQuick Check:
5 billion < long max [OK]
- Choosing int or uint which are too small
- Using short which is very small
- Not considering signed vs unsigned range
Ready to prove it?
Pass the test and this pattern moves unknown
