RingDotNetfor Ring

Number systems and big numbers

Binary, octal and hexadecimal, bits and shifts, and numbers too large for a normal Ring number. This lesson shows how to convert between bases, why hexadecimal is everywhere, and where a number stops being exact.

What you will learn#

Bases#

The number two hundred fifty-five can be written in any base:

BaseNameDigits255 is
2binary0 111111111
8octal0–7377
10decimal0–9255
16hexadecimal0–9 a–fff

Computers store bits, so base 2 is what they use; but it is long to read. One hexadecimal digit is exactly 4 bits, so a byte is exactly two hex digits (00 to ff). That is why colors (#ff8000), memory addresses, hashes and byte dumps (the tutorial Binary files) are written in hex.

The program#

samples/UsingDotNet/tutorials/ch03_bases.ring
# number systems and big numbers - binary, hexadecimal, bits, and numbers that do not fit in a double
load "dotnet.ring"

using {
	"System"
	"System.Numerics"
}

func main

	oConvert = new Convert()

	# 1. The same number in different bases: 2 (binary), 8 (octal), 10 (decimal), 16 (hexadecimal)
	? "1. 255 in base 2 : " + oConvert.Format([ :value = 255, :toBase = 2 ])
	? "   255 in base 8 : " + oConvert.Format([ :value = 255, :toBase = 8 ])
	? "   255 in base 16: " + oConvert.Format([ :value = 255, :toBase = 16 ])

	# 2. And back: a text in a base to a number
	? nl + "2. 'ff' in base 16       = " + oConvert.ToInt32([ :value = "ff", :fromBase = 16 ])
	? "   '11111111' in base 2 = " + oConvert.ToInt32([ :value = "11111111", :fromBase = 2 ])
	? "   '777' in base 8      = " + oConvert.ToInt32([ :value = "777", :fromBase = 8 ])

	# 3. Why hexadecimal? One hex digit is exactly 4 bits, so a byte is two digits. Colors, memory addresses and hashes use it.
	nRed = 255
	nGreen = 128
	nBlue = 0
	? nl + "3. the color (255,128,0) is #" + hex2(nRed) + hex2(nGreen) + hex2(nBlue)
	? "   #1e90ff is (" + oConvert.ToInt32([ :value = "1e", :fromBase = 16 ]) + "," + oConvert.ToInt32([ :value = "90", :fromBase = 16 ]) + "," + oConvert.ToInt32([ :value = "ff", :fromBase = 16 ]) + ")"

	# 4. Bits: count the ones, test if a number is a power of two, shift
	oBits = new BitOperations()
	? nl + "4. the number of 1 bits in 255: " + oBits.PopCount(255) + ", in 256: " + oBits.PopCount(256)
	? "   is 64 a power of 2? " + yesNo(oBits.IsPow2(64)) + ", is 100? " + yesNo(oBits.IsPow2(100))
	? "   2 to the power of 10 = " + (1 << 10) + ", and 1024 / 2^3 = " + (1024 >> 3)

	# 5. A negative number in binary: the computer uses "two's complement", all the bits are turned over
	? nl + "5. -5 as 32 bits: " + oConvert.Format([ :value = -5, :toBase = 2 ])

	# 6. The size of a Ring number: a double is exact only up to 2^53 = 9007199254740992
	? nl + "6. 9007199254740992 + 1 = " + (9007199254740992 + 1) + "  (the +1 is lost)"
	? "   Int64.MaxValue should be 9223372036854775807, Ring shows " + new Int64().MaxValue

	# 7. BigInteger: any size, always exact. Make it from a text and call its methods.
	oBig = new BigInteger().Parse("123456789012345678901234567890")
	oSquare = oBig.Multiply(oBig,oBig)
	? nl + "7. the square of 123456789012345678901234567890 is"
	? "   " + oSquare.ToString()

	# 8. A factorial with big numbers: 30! does not fit in a double, but it fits in a BigInteger
	oFactorial = new BigInteger().Parse("1")
	for nI = 2 to 30
		oFactorial = oFactorial.Multiply(oFactorial,new BigInteger().Parse("" + nI))
	next
	? nl + "8. 30! = " + oFactorial.ToString()

func hex2 nValue

	cText = new Convert().Format([ :value = nValue, :toBase = 16 ])
	if len(cText) = 1
		cText = "0" + cText
	ok
	return cText

func yesNo lValue

	if lValue
		return "yes"
	ok
	return "no"
Output
1. 255 in base 2 : 11111111
   255 in base 8 : 377
   255 in base 16: ff

2. 'ff' in base 16       = 255
   '11111111' in base 2 = 255
   '777' in base 8      = 511

3. the color (255,128,0) is #ff8000
   #1e90ff is (30,144,255)

4. the number of 1 bits in 255: 8, in 256: 1
   is 64 a power of 2? yes, is 100? no
   2 to the power of 10 = 1024, and 1024 / 2^3 = 128

5. -5 as 32 bits: 11111111111111111111111111111011

6. 9007199254740992 + 1 = 9007199254740992.00  (the +1 is lost)
   Int64.MaxValue should be 9223372036854775807, Ring shows 9223372036854775808.00

7. the square of 123456789012345678901234567890 is
   15241578753238836750495351562536198787501905199875019052100

8. 30! = 265252859812191058636308480000000

How it works#

Converting#

Convert.ToString(number, base) writes a number in a base and Convert.ToInt32(text, base) reads it. In Ring the two-argument forms take a list of named values: Format([ :value = 255, :toBase = 16 ]) and ToInt32([ :value = "ff", :fromBase = 16 ]) (ToString with parameters is Format). A color is three numbers: hex2 writes each as two hex digits, with a leading zero when needed, and the reverse takes each pair of digits.

Bits#

BitOperations.PopCount(n) counts the one-bits (255 has 8, 256 has 1) and IsPow2(n) tests for a power of two — a number that is a power of two has exactly one bit set. In Ring, 1 << 10 shifts the bits left (multiplies by 2 ten times: 1024) and 1024 >> 3 shifts right (divides by 8). The operators &, | and ~ are explained in the tutorial Enums and flags.

A negative number such as −5 is stored in two's complement: write 5 (000…0101), turn all the bits over (111…1010) and add 1 (111…1011). The output of step 5 is exactly that, 32 bits long.

Where Ring numbers stop#

A Ring number is a 64-bit double. It has 53 bits for the digits, so it is exact only up to 2⁵³ = 9,007,199,254,740,992. Above that, + 1 can be lost (step 6), and Int64.MaxValue (9,223,372,036,854,775,807) is shown rounded. For a bank balance, an id or a hash that is bigger than that, use text or BigInteger.

BigInteger#

System.Numerics.BigInteger has no size limit and is always exact. Create it from text (Parse) and call its methods (Multiply, Add, Pow...); ToString() returns the digits. Step 7 squares a 30-digit number; step 8 computes 30! (265252859812191058636308480000000, 33 digits), which a double could only approximate.

Note

Pass big numbers around as text. If a value crosses the bridge as a Ring number it becomes a double and loses its exactness.

What you learned#

Exercises#

  1. Write toBinary(n) yourself with division by 2 and compare with Convert.
  2. Compute 100! and count its digits.
  3. Parse a color like #1e90ff into three numbers and back.
  4. Find the largest power of two below 10 million with PopCount or IsPow2.

Next#

Money

RingDotNet — use .NET classes and methods from Ring programs. Screenshots and output on these pages come from the real samples.