Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8752425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:14:03+00:00 2026-06-13T13:14:03+00:00

In some PHP I need to compare two strings, but only on the bits

  • 0

In some PHP I need to compare two strings, but only on the bits that are set as one in the bitmask. How would I implement such a behavior?

I’ve tried:

$string1='aaabbb';
$string2='ababbb';
$bitmask='101101';
function compare($string1, $string2, $bitmask){
    $resultBitmask=(~($string1 ^ $string2)|~$bitmask);
} 

For clarity’s sake, I’ve written ff bytes as 1 in the bitmask for illustrative purposes. They would actually be ff in hex when a bitmask is generated. Same goes for 0 being null bytes.

The string and the bitmask are always different lengths each time the function is called. I’ve managed to get a set of bits for comparison, but am unable to check whether they are all set since the lenths differ. At this time, I’ve been using preg_match with a regex that matches any number of ff bytes, but is there a more elegant solution?

Edit: Since the strings are any length up to 4096 bits long, they cannot be converted to numbers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T13:14:04+00:00Added an answer on June 13, 2026 at 1:14 pm

    Self-solved:

    Since this will repeat with many strings of the same length during a run, but have different lengths between runs, I need to check that the resulting string after the bitwise operations is all ones and the correct length. I realized that this string full of ones can be generated when needed, which is quite rarely, once every 1000 or so string comparisons. I can generate the string before runs as follows:

    $ones=str_repeat(chr(255), $byte_length);
    

    and then defining the compare( function a bit differently:

    function compare($string1, $string2, $bitmask){
        global $ones;
        $resultBitmask=(~($string1 ^ $string2)|~$bitmask);
        if ($resultBitmask=$ones){
             return 1;
        } else {return 0};
    } 
    

    The trick was the str_repeat which I was not aware of before.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some PHP classes that deal with image processing in a good manner.
I need some help with a small php problem. But i dont know how
My employer needs a change to some PHP files that lead me to need
I need some help with some PHP and MySQL code. At the moment I
I have a bash script which need to execute some php scripts and to
I'm planning on making some dynamic PHP websites and I need a free Authentication
Im working on a shopping cart script in php and need some advice on
I need some help with PHP Class. I have limited knowledge about how it
I need to rewrite some Python code into PHP (don't hate me, a customer
I need some comparing and replacing code in PHP Our mission is to empower

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.