Document

advertisement
No. 10042
Subject : Number DataType
------------------------This article attempts to explain, with examples, the ORACLE Number datatype (type 2) as stored
internally. Oracle Corporation reserves the right to
any time without reason and notice.
change this internal representation at
Numeric data is stored internaly in variable length format,
and consists of two parts
- SIGN/EXPONENT Byte
(1st byte)
- DATA Byte(s)
(2nd byte onwards)
SIGN/EXPONENT BYTE : The sign/exponent byte is divided into two parts again
- Sign bit (high-order bit)
- Exponent bits (lower order 7 bits)
The key to the calculation of a number is the sign bit which can take values 0 for negative
and 1 for positive numbers.
The 7 lower order bits are the exponent, stored in excess 64 notation i.e. whatever the number
you calculate from the 7 bits, you will have to deduct 64 from it to determine exponent value.
The Sign/Exponent byte is evaluated as follows -
If the sign bit is set then the number is positive and the exponential is calculated in the
following manner Determine the value of the exponent using the lower order 7 bits. As this value
is in excess 64 notation, deduct 64 from this number that was calculated to give the exponent
value.
E.g. Assume the SIGN/EXPONENT byte was 223 which in binary is 11011111. From the binary
representation of the example we determine that bit 8 is set, hence this is a positive number.
To determine the
exponent, clear bit 8 and then calculate the resultant which in this case is
95. As 95 is in excess 64 notation deduct 64 from it to give 31, which is the exponent.
If the sign bit is cleared i.e. 0 (number is negative), a little more work goes into
calculating the number. Perform a 1's complement of the sign/exponent byte (all 8 bits)
Determine the value of the exponent using the lower order 7 bits. As
this value is in excess 64
notation, deduct 64 from the value that was calculated to give the exponent value. E.g. Assume
the SIGN/EXPONENT byte was 100 which in binary is 01100100. From the binary representation we
can determine that the number is negative as bit 8 is clear. Now take the 1's complement of
01100100 to give 10011011. Mask of bit 8 (or just set it to 0) so that only bits 1 through 7 are
used to calculate the exponent which in our example will be 27. As 27 is in excess 64 notation,
deduct 64 from it to give -37. Hence, exponent is -37.
DATA BYTE(s) : Data is stored in a series of bytes with the representation
being in base 100.
To avoid a binary zero a 1 is added to the number. Hence, each data byte represents digits in
base 100 with values ranging from 1 to 100 representing values 0 to 99. Negative numbers have a
terminator byte with a value of 102.
Let us take some examples and then try to understand the methodology of the calculation. These
examples work off a table with the following description.
SQL> describe num_tab
Name
Null?
Type
------------------------------- -------- ---C1
NUMBER
CASE 1 : insert number 123456. Dump of column c1 gives the following
DUMP(C1)
-----------------------------------------Typ=2 Len=4: 195,13,35,57
1st byte = 195 decimal --> 11000011 binary
Number is positive as bit 8 is set. The exponent value is 01000011 binary which is 67 decimal.
As this number is in excess 64 notation
deduct 64 from 67 giving 3. Exponent is 3 and positive.
2nd byte = 13 decimal
As the sign is positive deduct 1 from 13 (as it is stored with one added) to get 12
3rd byte = 35 decimal
As the sign is positive deduct 1 from 35 to get 34
4th byte = 57 decimal
As the sign is positive, deduct 1 from 57 to get 56
The exponent is 3, the data bytes are in base 100, so the number is
12 x (100 e 2) + 34 x (100 e 1) + 56 x (100 e 0) = 123456
CASE 2 : insert number -123456. Dump c1 gives the following
DUMP(C1)
------------------------------------------
Typ=2 Len=5: 60,89,67,45,102
1st byte = 60 decimal --> 00111100 binary
Number is negative as bit 8 is not set. Take the 1's complement of 00111100 which is 11000011.
From this the exponent is 67 decimal (take the lower order 7 bits to calculate the exponent). As
this number is
in excess 64 notation deduct 64 from 67 giving 3. Exponent is positive
and
is 3.
2nd byte = 89 decimal
Deduct 1 from this which gives 88. As the sign is negative, you need
complement of this i.e. 100 - 88
to take the 100's
to get 12.
3rd byte = 67 decimal
Deduct 1 from this which gives 66 and take the 100's complement to get 34.
4th byte = 45 decimal
Deduct 1 from this which gives 44 and take the 100's complement to get 56.
5th byte = 102 decimal
This is the last byte.
The calculation is
12 x (100 e 2) + 34 x (100 e 1) + 56 x (100 e 0) = 123456
and as the sign is negative, the answer is -123456.
CASE 3 : insert number 123456.789. Dump (c1) gives the following
DUMP(C1)
-----------------------------------------Typ=2 Len=6: 195,13,35,57,79,91
1st byte = 195 --> 11000011 binary
Number is positive as bit 8 is set. The exponent value is 01000011 binary which is 67 decimal.
As this number is in excess 64 notation deduct 64 from 67 giving 3
2nd byte = 13 decimal
As the sign is positive, deduct 1 from 13 to get 12.
3rd byte = 35 decimal
As the sign is positive, deduct 1 from 35 to get 34.
4th byte = 57 decimal
which is a positive number.
As the sign is positive, deduct 1 from 57 to get 56.
5th byte = 79 decimal
As the sign is positive, deduct 1 from 79 to get 78.
6th byte = 91 decimal
As the sign is positive, deduct 1 from 91 to get 90. The exponent is 3, the data bytes are in
base 100, so the number is
12 x (100 e 2) + 34 x (100 e 1) + 56 x (100 e 0)
+ 78 x (100 e -1) +
90 x (100 e -2) = 123456.789
CASE 4 : insert number -123456.789 Dump of c1 gives the following
DUMP(C1)
-----------------------------------------Typ=2 Len=7: 60,89,67,45,23,11,102
1st byte = 60 decimal --> 00111100 binary
Number is negative as bit 8 is not set. Take the 1's complement of
00111100 which is 11000011.
From this the exponent is 67 decimal(take the lower order 7 bits to calculate the exponent). As
this number is in excess 64 notation deduct 64 from 67 giving 3. Exponent is positive and is 3.
2nd byte = 89 decimal
Deduct 1 from this which give 88. As the sign is negative, you need to take the 100's
complement of this i.e. 100 - 88
to get 12.
3rd byte = 67 decimal
Deduct 1 from this which give 66. As the sign is negative, you need to take the 100's
complement of this i.e. 100 - 66
to get 34.
4th byte = 45 decimal
Deduct 1 from this which give 44. As the sign is negative, you need to take the 100's
complement of this i.e. 100 - 44
to get 56.
5th byte = 23 decimal
Deduct 1 from this which give 22. As the sign is negative, you need to take the 100's
complement of this i.e. 100 - 22
to get 78.
6th byte = 11 decimal
Deduct 1 from this which give 11. As the sign is negative, you need to take the 100's
complement of this i.e. 100 - 11
to get 89.
7th byte = 102 decimal
This is the last byte.
The calculation is
12 x (100 e 2) + 34 x (100 e 1) + 56 x (100 e 0)
+ 78 x (100 e -1) +
90 x (100 e -2) = 123456.789
and as the sign is negative, the answer is -123456.789
The above explanation is intended to further the knowledge of ORACLE's internal datatype
representation only.
Note : Given below are examples of calculating the 1's and 100's complement.
1's complement of 10001100 = 01110011 (just 'not' the bits) 100's
complement of 26
= 100 - 26 = 74
Download