[ create a new paste ] login | about

Link: http://codepad.org/EnwJ7WN4    [ raw code | fork ]

PHP, pasted on Jun 16:
function calculateMaxPower($value) {
    //calculates the minimum power of 2 that the input value can fit into, i.e.
    //the maximum power of 2 necessary to contain the input value
    
    $max_power = 0;
    
    //initial calculated power of 2 should be large enough to contain the entire input value
    while(pow(2, $max_power) < $value) {
        $max_power++;
    }
    
    //if the input value is not a power of 2, then we have a power value that exceeds our needs by 1
    if(pow(2, $max_power) != $value) {
        $max_power--;
    }
    
    return $max_power;
}

function integerToBinaryArray($value, $max_power) {
    //generates an array for the binary representation of an integer using big-endian ordering
    
    //pre-fill the binary array with 0s
    $binary_array = array_fill(0, $max_power + 1, 0);
    
    //calculate the binary representation
    $remainder = $value;
    for($i = $max_power; $i >= 0; $i--) {
        //if a power of 2 for the current power doesn't fit within the remainder, then move on to the next power
        $calculated_power_value = pow(2, $i);
        $this_power_fits_within_remainder = $calculated_power_value - $remainder <= 0;
        if(!$this_power_fits_within_remainder) continue;
        
        //otherwise, reduce the remainder by the calculated value and mark the current bit position
        $remainder -= $calculated_power_value;
        $binary_array[$max_power - $i] = 1;
    }
    
    return $binary_array;
}

function binaryArrayToInteger($binary_array) {
    //generates an integer from a binary array using big-endian ordering
    
    $result = 0;
    $max_power = count($binary_array) - 1;
    for($i = $max_power; $i >= 0; $i--) {
        //for every bit position marked with a 1, add the appropriate power of 2 to our total
        $result += $binary_array[$max_power - $i] == 1 ? pow(2, $i) : 0;
    }
    
    return $result;
}

function performBitwiseOperation($value_1, $value_2, $binary_comparison_callback) {
    //find the most significant bit needed to represent the larger of the two values
    $largest = max($value_1, $value_2);
    $overall_max_power = calculateMaxPower($largest);
    
    //generate the appropriate binary representations of the input values
    $value_1_binary = integerToBinaryArray($value_1, $overall_max_power);
    $value_2_binary = integerToBinaryArray($value_2, $overall_max_power);
    
    //prefill the resulting binary array with 0s
    $result_binary = array_fill(0, $overall_max_power + 1, 0);
    for($i = 0; $i <= $overall_max_power; $i++) {
        //for every bit position where both binary arrays contain a 1, mark the resulting bit position
        $result_binary[$i] = $binary_comparison_callback($value_1_binary[$i], $value_2_binary[$i]) ? 1 : 0;
    }
    
    //return the integer representation of the resulting binary array
    return binaryArrayToInteger($result_binary);
}

function BITWISE_AND($value_1, $value_2) {
    //calculate the bitwise AND of two integer values, assuming non-negative values, without the use of existing bitwise operators
    
    $binary_comparison_callback = function($binary_value_1, $binary_value_2) {
        return $binary_value_1 == 1 && $binary_value_2 == 1;
    };

    return performBitwiseOperation($value_1, $value_2, $binary_comparison_callback);
}

function BITWISE_OR($value_1, $value_2) {
    //calculate the bitwise OR of two integer values, assuming non-negative values, without the use of existing bitwise operators

    $binary_comparison_callback = function($binary_value_1, $binary_value_2) {
        return $binary_value_1 == 1 || $binary_value_2 == 1;
    };

    return performBitwiseOperation($value_1, $value_2, $binary_comparison_callback);
}

function BITWISE_XOR($value_1, $value_2) {
    //calculate the bitwise XOR of two integer values, assuming non-negative values, without the use of existing bitwise operators

    $binary_comparison_callback = function($binary_value_1, $binary_value_2) {
        return $binary_value_1 != $binary_value_2;
    };

    return performBitwiseOperation($value_1, $value_2, $binary_comparison_callback);
}

function BITWISE_NOT($value) {
    //calculate the bitwise NOT of an integer value without the use of existing bitwise operators

    return -($value + 1);
}


Create a new paste based on this one


Comments: