Sonntag, 8. März 2015

In case of 16-bit, the largest value for an integer is 215-1=32,767....Wait, what?


215-1=32,767...


I signed up for an open university course about Imperative Programming using Pascal. Lecture 1 introduces the integer data type.
"In case of 16-bit, the largest value for an integer is 215-1=32,767 (maxint)."
"The range of possible integer values is [-maxint - 1, maxint]." 
No further explanation given. I picked a couple of programming language books more or less randomly from my bookshelf to check if any of those do a better job at clarifying the upper and lower bounds of a given data type:



The Java Tutorial - A Short Course on the Basics
"The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive)." 
Programming Ruby
"Integers within a certain range (normally - 230 to 230 - 1 or -262 to 262 - 1) are held internally in binary form...."
Head First C#
"int is commonly used for whole numbers. It holds numbers up to 2,147,483,647."
You get the idea. Introduce a datatype. Mention the upper an lower bounds. Read, forget, look up again if ever necessary. No deeper understanding gained.

"Objective-C Programming The Big Nerd Ranch Guide" does a slightly better job:
"An unsigned 8-bit number can hold any integer from 0 to 255. Why? 28 = 265 possible numbers. And we choose to start at 0."
"A signed 64-bit number can hold any integer from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. One bit for the sign leaves 263 = 9,223,372,036,854,775,808. There is only one zero."

As Big Nerd Ranch's book goes a bit deeper on the subject on how he upper and lower bounds of a datatype are determined based on the number of bits, I assume that I am not the only one out there who doesn't get this right away. I had to explain this to myself...

One more bit...


Let's see. I start simple with a single bit. Its value can be either 0 or 1.
This makes room for two decimal numbers (0, 1).

Binary Decimal
0 0
1 1

I add one more bit.
With two bits it's enough space for four decimal numbers (0...3).

Binary Decimal
00 0
01 1
10 2
11 3

And one more bit.
Three bits can store up to eight decimal numbers (0...7).

Binary Decimal
000 0
001 1
010 2
011 3
100 4
101 5
110 6
111 7

As every additional bit doubles the amount of numbers that can be stored, this is a 2n relation.
2 for the number of possible values a bit can hold (either 0 or 1), n for the amount of available bits.



Upper and lower bounds...


Back to the original Pascal language example. The upper bound for an integer was given as 32,767 (maxint) in the courseware, the lower bound as -32,768 (-maxint - 1). Where do these values come from?

So far I was only looking at positive values including zero. 16 bits would make 216 = 65,536.

What about negative values? The binary system allows the representation of negative (and positive) numbers without requiring additional symbols like "+" or "-". Instead the highest bit is used to indicate wether the number is either a positive or negative one (signed). The value of the highest bit is 0? We are dealing with zero or a positive number. The value of the highest bit is 1? Must be a negative number.

Examples:
  • 0000000000000001: positive number with value 1. 
  • 1000000000000000: negative number with value -1. 

Because in case of negative numbers the value of the highest bit is always set to 1, we have fifteen (16 - 1) remaining slots to fill. 215 = 32,768.

This would cover the range of 0 to -32,767. As by definition zero is part of the positive numbers, we have one additional slot and can go from -1 to -32,768 (-32,767 - 1), which makes -32,768 the lowest number possible (minimum).

Same for positive numbers. The highest bit is occupied by 0. Fifteen remaining slots to fill as well. 215 = 32768. This covers the range of 0 to 32767. Therefore 32767 is the highest number possible (maximum) as we must include the zero.

Positive numbers:

Binary Decimal
0000000000000000 0
0000000000000001 1
... ...
0111111111111110 32,766
0111111111111111 32,767

Negative numbers:

Binary Decimal
1000000000000000 -1
1000000000000001 -2
... ...
1111111111111110 -32,767
1111111111111111 -32,768

Keine Kommentare:

Kommentar veröffentlichen