Skip to content

Instantly share code, notes, and snippets.

@jaytaph
Forked from Dynom/shiftin.php
Created February 11, 2013 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaytaph/4754691 to your computer and use it in GitHub Desktop.
Save jaytaph/4754691 to your computer and use it in GitHub Desktop.
<?php
/**
* @author Mark van der Velden <mark@dynom.nl>
*/
/*
* Given, the following specification:
* - part 1, 40 bits wide -- a number between 0 - 1099511627775
* - part 2, 5 bits wide -- a number between 0 - 31
* - part 3, 9 bits wide -- a number between 0 - 511
* - part 4, 10 bits wide -- a number between 0 - 1023
*
* How to extract all the parts?
*/
// Creating the number -------------------------------------------------------------------------------------------------
// Part 1, 40 bits.
// Creating an integer that will fill the left-most 40 bits.
// This part is the amount of milliseconds, since januari 1st, 2013.
$time = (int) ((microtime(true) - mktime(0, 0, 0, 1, 1, date("Y") - 34)) * 1000);
// Part 2, 5 bits
// Creating an arbitrary number
$dcId = 0;
// Part 3, 9 bits
// Creating yet another arbitrary number
$nId = 511;
// Part 4, 10 bits - Milliseconds fraction
$msf = 0; //1023; //$time % 1024;
$id = $time << 24; // 64 - 40 = 24
$id |= $dcId << 19; // (64 - (40 + 5)) = 19
$id |= $nId << 10; // (64 - (40 + 5 + 9)) = 10
$id |= $msf;
printf("\n%064b (size is %d)", $id, strlen(sprintf("%064b", (~$id)+1)));
printf("\n%064b (size is %d)", $id, strlen(sprintf("%064b", $id)));
printf("\n%s%s - Millisecond fraction", str_repeat(' ', 40+5+9), str_repeat('^', 10));
printf("\n%s%s - Network ID", str_repeat(' ', 40+5), str_repeat('^', 9));
printf("\n%s%s - Data center", str_repeat(' ', 40), str_repeat('^', 5));
printf("\n%s - millisecond timestamp", str_repeat('^', 40));
echo "\n";
$part1 = $id >> (64 - 40) & 1099511627775;
$part2 = ($id >> (64 - (40 + 5))) & 31;
$part3 = ($id >> (64 - (40 + 5 + 9))) & 511;
$part4 = ($id >> (64 - (40 + 5 + 9 + 10))) & 1023;
printf("\n%040b %05b %09b %010b", 1099511627775, 31, 511, 1023);
printf("\n%040b %05b %09b %010b", $part1, $part2, $part3, $part4);
echo "\n\n";
echo "Part 1 $part1 = $time\n";
echo "Part 2 $part2 = $dcId\n";
echo "Part 3 $part3 = $nId\n";
echo "Part 4 $part4 = $msf\n";
var_dump(
(~$id) + 1,
PHP_INT_MAX
);
/*
Output:
1111101010101010010000100011111000001010000001111111110000000000 (size is 64)
1111101010101010010000100011111000001010000001111111110000000000 (size is 64)
^^^^^^^^^^ - Millisecond fraction
^^^^^^^^^ - Network ID
^^^^^ - Data center
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - millisecond timestamp
1111111111111111111111111111111111111111 11111 111111111 1111111111
1111101010101010010000100011111000001010 00000 111111111 0000000000
Part 1 1076598291978 = 1076598291978
Part 2 0 = 0
Part 3 511 = 511
Part 4 0 = 0
int(384421983963055104)
int(9223372036854775807)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment