Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

167
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda