Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

166
Views
convert this function from javascript to php

need this function in php :/ and i'm struggling with >>>= operators!? please help to convert this javascript function to php - thank you <3

function calcValues(variant) {
    var shape = variant & 0xFF;
    variant >>>= 8;
    var pattern = variant & 0xFF;
    variant >>>= 8;
    var baseColor = variant & 0xFF;
    variant >>>= 8;
    var patternColor = variant & 0xFF;

    console.log(shape, pattern,baseColor,patternColor);
}    
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

<?php
function calcValues($variant) {
    $shape = $variant & 0xFF;
    $variant=$variant >>  8;
    $pattern = $variant & 0xFF;
    $variant=$variant >> 8;
    $baseColor = $variant & 0xFF;
    $variant=$variant >> 8;
    $patternColor = $variant & 0xFF;

    //console.log($shape, $pattern,$baseColor,$patternColor);
    var_dump([$shape, $pattern,$baseColor,$patternColor]);
}
calcValues(65535);
var_dump(0xFF);
?>

by mistake i deleted the "$variant= " .. this edited version gives me 255 255 0 0 (the same result for your javascript version ,using 65535 value)

here you have the complete guide of other functions https://www.php.net/manual/en/language.operators.bitwise.php

about 4 years ago · Juan Pablo Isaza Report

0

IMO Constantin's answer is objectively best for OP's specific case. But...

While I maintain that you don't need >>> equivalent for the given code, and >> will work just fine, and have the further warning that using a >>> equivalent on negative integer values across languages/systems with different integer/word sizes will have undefined behaviour, here is a function that replicates the behaviour of JS's >>> operator.

// formatted decbin
function fdecbin($input) {
    $out = decbin($input);
    $len = ((int)ceil(strlen($out)/8))*8;
    $out = str_pad($out, $len, '0', STR_PAD_LEFT);
    return implode(' ', str_split($out, 8));
}

// zero-padded right bit shift
function zeroshift($input, $bits) {
    if( $bits == 0 ) { return $input; }
    $mask = PHP_INT_MAX >> ($bits - 1);
    return $input >> $bits & $mask;
}

$i = -110;

var_dump(
    fdecbin($i),
    fdecbin($i>>2),
    fdecbin(zeroshift($i, 2))
);

Output:

string(71) "11111111 11111111 11111111 11111111 11111111 11111111 11111111 10010010"
string(71) "11111111 11111111 11111111 11111111 11111111 11111111 11111111 11100100"
string(71) "00111111 11111111 11111111 11111111 11111111 11111111 11111111 11100100"

Again, please carefully consider what the code is actually doing before using an obscure, esoteric operator/operation like >>>, or this code that replicates it.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!