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 141093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:43:16+00:00 2026-05-11T07:43:16+00:00

I´m trying to break a number into an array of numbers (in php) in

  • 0

I´m trying to break a number into an array of numbers (in php) in the way that for example:

  • 25 becomes (16, 8, 1)
  • 8 becomes (8)
  • 11 becomes (8, 2, 1)

I don´t know what the correct term is, but I think the idea is clear.

My solution with a loop is pretty straightforward:

   $number = rand(0, 128);        $number_array_loop = array();     $temp_number = $number;    while ($temp_number > 0) {        $found_number = pow(2, floor(log($temp_number, 2)));        $temp_number -= $found_number;             $number_array_loop[] = $found_number;    } 

I also have a recursive solution but I can´t get that to work without using a global variable (don´t want that), the following comes close but results in arrays in arrays:

   function get_numbers($rest_number) {             $found_number = pow(2, floor(log($rest_number, 2)));         if ($found_number > 0) {            $temp_array[] = get_numbers($rest_number - $found_number);            $temp_array[] = $found_number;        }             return $temp_array;    }     $number_array_recursive = array();    $number_array_recursive = get_numbers($number); 

However, using something like pow(floor(log())) seems a bit much for a simple problem like this.

It seems to me that the problem calls for a recursive solution with some very simple maths, but I just don´t see it.

  • 1 1 Answer
  • 4 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. 2026-05-11T07:43:16+00:00Added an answer on May 11, 2026 at 7:43 am

    You can check each bit of the input number with the following (untested) function.

    function checkBit($var, $pos) {     return ($var & (1 << $pos)); } 

    It checks the bit at position $pos in the variable $var by using a bitwise AND function. I’ll show you with 4-bit numbers for brevity.

    • 1 = 0001
    • 2 = 0010
    • 4 = 0100
    • 8 = 1000

    If I want to check position 0 (the rightmost bit) of the number 3, I’d call the function like this:

    $number = 3; checkBit($number, 0); 

    Internally, checkBit is going to shift the constant 1 to the left 0 times because I passed in a 0. It’s then going to bitwise AND (&) the result with the number I passed in, 3. Since 3 = 0011 and 1 = 0001 the result is true, since the 0th bit is set in both arguments to the bitwise AND operator.

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

Sidebar

Related Questions

Using PHP, I'm trying to break an array up into multiple arrays based on
I need to know how you break an array into sub-arrays based on the
Im trying to break a while loop before it goes into the negative numbers
I'm trying to find the best way to split data into groups, for example
I'm trying to create an method to evenly distribute an array into X numbers
I'm trying to break up a square or rectangle into a large number of
I am trying to break a huge SQL file into little sql file and
I'm trying to find a way to break a string at the second last
I'm trying to get a PHP script to split a list of numbers separated
m trying to break one large Excel spreadsheet into several. I've made good progress,

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.