```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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 ``` ```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); } ```