InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Calculating Using Numbers in Bases Other Than Decimal in PHP
PHP

Calculating Using Numbers in Bases Other Than Decimal in PHP

InfinityCoder November 17, 2016

You want to perform mathematical operations with numbers formatted not in decimal, but in octal or hexadecimal. For example, you want to calculate web-safe colors in hexadecimal.

Prefix the number with a leading symbol, so PHP knows it isn’t in base 10.

The leading symbol 0b indicates binary (base 2), the leading symbol 0 indicates octal (base 8) and the leading symbol 0x indicates hexadecimal (base 16). If $a = 100 and $b = 0144 and $c = 0x64 and $d = 0b1100100, PHP considers $a, $b, $c, and $d to be equal.

Here’s how to count from decimal 1 to 15 using hexadecimal notation:

1
2
3
for ($i = 0x1; $i < 0x10; $i++) {
    print "$i\n";
}

Even if you use hexadecimally formatted numbers in a for loop, by default all numbers are printed in decimal. In other words, the code in the Solution doesn’t print out …, 8,9, a, b, ….

To print in hexadecimal, use one of the methods listed . Here’s an example:

1
for ($i = 0x1; $i < 0x10; $i++) { print dechex($i) . "\n"; }

For most calculations, it’s easier to use decimal. Sometimes, however, it’s more logical to switch to another base—for example, when doing byte arithmetic.

Dan Bernstein’s popular “times 33” hash is a convenient and fast way to hash a string of arbitrary length to an integer value.

To compute the “times 33” hash, you start with the magic number 5381 as your hash value. Then, for each byte in the string you want to hash, you add the byte and the previous hash value times 32 to the hash value.

Translating that directly to PHP produces code that looks like this:

1
2
3
4
5
6
7
8
function times_33_hash($str) {
   $h = 5381;
   for ($i = 0, $j = strlen($str); $i < $j; $i++) {
       // Shifting $h left by 5 bits is a quick way to multiply by 32
       $h += ($h << 5) + ord($str[$i]);
   }
   return $h;
}

That code isn’t completely correct, however. It produces some strange results. For example, times_33_hash(“Once, I ate a papaya.”) returns a float, not an integer, with a really, really large value (about 2.28375 x 1019).

The repeated multiplications and additions, once for each byte in the string, have overflowed PHP’s maximum integer value so PHP’s autoconversion to float (with loss of precision) kicked in.

To fix this, all you have to do is logical-AND the hash value with a mask of the significant bits you want to keep in the hash value.

Expressing those significant bits is a lot more understandable in hexadecimal rather than decimal.

For example, if you want 32 bits in the hashed value, add a masking line inside the loop as follows:

1
2
3
4
5
6
7
8
9
10
function times_33_hash($str) {
   $h = 5381;
   for ($i = 0, $j = strlen($str); $i < $j; $i++) {
       // Shifting $h left by 5 bits is a quick way to multiply by 32
       $h += ($h << 5) + ord($str[$i]);
       // Only keep the lower 32 bits of $h
       $h = $h & 0xFFFFFFFF;
   }
   return $h;
}

Each hexadecimal F represents four bits, so masking with eight of them produces a 32- bit mask.

You could use 4294967295 in your code as the mask value instead of 0xFFFFFFFF, but it wouldn’t be as clear.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Sharing Encrypted Data with Another Website in PHP
You want to exchange data securely with another website. If …

Sharing Encrypted Data with Another Website in PHP

Protecting Against Form Spoofing in PHP
You want to be sure that a form submission is …

Protecting Against Form Spoofing in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap