multiint-snippertje

Gesponsorde koppelingen

PHP script bestanden

  1. multiint-snippertje

« Lees de omschrijving en reacties

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php/*
Function to store multiple integers in a single integer.
INPUT:
    $target: MIXED representing an INT or an ARRAY.
        When an INT is given, it will be extracted to multiple ints. OUTPUT will be an ARRAY containing the integers.
        When an ARRAY is given, it will be stored to a single int. OUTPUT will be an INT.
    $layout: ARRAY containing multiple integers representing the reserved amount of bits per integer.

OUTPUT:
    MIXED: ARRAY or INT.

NOTES:
    -You cannot store more then 32 bits of data.
        Please don't try it: something unknown will happen. Seriously, i don't know what will happen.
        This is what PHP.net says about shifting more than 32 bits of data: ( http://nl2.php.net/language.operators.bitwise )
        "Warning: Don't right shift for more than 32 bits on 32 bits systems. Don't left shift in case it results to number longer than 32 bits."
        So don't try it: it may blow up your CPU! if you CANNOT resist to try it: Try at your own risk! Remember to wear safetly glasses in case the CPU explodes in your face.
    -If you store 32 bits of data, the integer may be a negative. Storing 31 bits or less should result in a positive integer.
    -If an INT is out of range, it will be rounded to the highest possible value. Negative numbers will rounded to 0.
    
Examples:
    
    $int =   multiint( array(3,1), array(2,1)); //$int = 7; (binary 111)
    $multi = multiint( $int      , array(2,1)); //$multi = array(2,1);
    
    $int =   multiint( array(28,3), array(5,3));     //$int = 115; (binary 11100011)
    $multi = multiint( $int       , array(2,2,2,2)); //$multi = array(3,2,0,3);
    
    $user_info = array(
        'age' => 44,
        'registered' => 1,
        'status' => 7,
    );
    $user_info_layout = array(
        'age' => 7,  //range: 0-127 ( 2^7-1 = 127)
        'registered' => 1,  //range: 0 or 1 ( 2^1-1 = 1)
        'status' => 3,  //range: 0-7 ( 2^3-1 = 8)
    );
    $dataInt =    multiint( $user_info , $user_info_layout); //$dataInt = 719 (binair: 1011001111)
    $user_info2 = multiint( $dataInt   , $user_info_layout); //$user_info2 = array('age' => 44,'registered' => 1,'status' => 7);
    echo $user_info2['age'];//will output "44".
*/
function multiint($target,$layout) {
    if(is_array($target)) {
        
        $output = 0;
        
        foreach($target as $key=>$int) {
            $inv = min((1<<$layout[$key])-1,max(0,(int)$int));
            $int = 0;
            for($x=0;$x<$layout[$key];$x++) {
                $int = ($int<<1) + ($inv&1);
                $inv = $inv >> 1;
            }
            for($x=0;$x<$layout[$key];$x++) {
                $output = ($output<<1) + ($int&1);
                $int = $int >> 1;
            }
        }
        
        return $output;
        
    } else {
        
        $target = (int)$target;
        $output = array();
        $layout = array_reverse($layout,true);
        
        foreach($layout as $key=>$itemlength) {
            $inv = 0;
            $output[$key] = 0;
            for($x=0;$x<$itemlength;$x++) {
                $inv = ($inv<<1) + ($target&1);
                $target = $target >> 1;
            }
            for($x=0;$x<$itemlength;$x++) {
                $output[$key] = ($output[$key]<<1) + ($inv&1);
                $inv = $inv >> 1;
            }
            
        }
        
        $output = array_reverse($output,true);
        
        return $output;
        
    }
}
?>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.