Site Moved

This site has been moved to a new location - Bin-Blog. All new post will appear at the new location.

Bin-Blog

Use of 'Numbers Particle' System in Programming

All numbers can be created using the series 1, 2, 4, 8, 16, 32, 64, 128, ... without repetition. For example, 7 can be represented as 1+2+4. 13 is 8+4+1. This property is used for creating binary number...


Decimal Numbers 12345678
Binary Numbers 00010010001101000101011001111000
Powers of 2 2021-22---23
Number Particles * 121+241+42+41+2+48

* For the lack of a better name. If you are aware of the correct terminology for this system, please let me know

An advantage of this series is that there is only one way to represent a number - ie. 7 can only be 1+2+4. In the normal series(1,2,3,4,5,6,7,8,...), 7 can be represented as 3+4, 5+2, 1+6, 1+2+4, etc. This property of the doubling number series can be used to store multiple values using just one number.

Example Usage

The best example for a use of this is the Linux File Permission system. In Linux...

  • 1 = Execute Permission
  • 2 = Write Permission
  • 4 = Read Permission

So if you want to give a file Read and Write permission it is 2 + 4 = 6. If you want Read and Execute permission it is 1 + 4 = 5.

Implementation

You can use this principle while designing a database. You can use this to store data that would otherwise require a reference table or multiple fields. For example, let us try to implement the linux user permissions system shown above using a database.

id1INT
filenameHello World.txtVARCHAR
is_writable1ENUM('1','0')
is_readable1ENUM('1','0')
is_executable0ENUM('1','0')

Actually, in linux, different permissions can be set for the file owner, the file owners group and the rest of the world. But I am ignoring that for simplecity.

In the above example, we use 3 fields for permissions. Using the 'number particle' method, we can reduce it to just one field.

id1INT
filenameHello World.txtVARCHAR
permission4INT

This method should not be used for a large dataset. Here the dataset is is write/read/execute - just 3. But if it is a larger dataset - like read/append/delete/modify/create/rename/copy/.... then using this system may not be the best method - as it will make the system more complicated. Even in small datasets, using this system will make things complicated. In most systems, I will not recommend using this method.

Another problem with this method is that the data is not atomic. So if you use this method, database normalization goes out the window.

Code

To use this method, you will need a method to decompose a number to its number particles. I have created such a function. Just give the number you want to decompose and it will return an array with the particles of the given number.

This code is in PHP - but feel free to translate it into other languages. If you have translated this into another languages, please leave the code as a comment.

function findParticles($number) {
 $all_particles = array();
 $series = 1;
 while($number) {
  if($number % 2) $all_particles[] = $series;//If the number is odd
  $number = intVal($number / 2);
  $series = $series * 2;
 }
 return $all_particles;
}

//Demo
$particles = findParticles(21); // Returns Array(1,4,16)

This is distributed under the terms of the BSD License.

Filed Under...

3 Comments:

Anonymous said...

«only one way to represent a number»?

14=8+4+2
14=8+4+1+1

Binny V A said...

Only one way to represent a number - without repeatation

In the example you gave, 1 is repeated.

Jean-Luc Picard said...

Very educational!