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

  • Home
  • SEARCH
  • 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
  • 1 View
  • 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

Ask A Question

Stats

  • Questions 277k
  • Answers 277k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I think if you just eliminate the "exec" altogether it… May 13, 2026 at 2:58 pm
  • Editorial Team
    Editorial Team added an answer Here is a good beginner's tutorial for mod_python. As far… May 13, 2026 at 2:58 pm
  • Editorial Team
    Editorial Team added an answer Double pointer is a pointer to pointer. why is the… May 13, 2026 at 2:58 pm

Related Questions

I'm having a problem with my looping over a vector, and deleting values from
I´m trying to separate a gridViewColumn into two rows. Im using default style for
I´m trying to expose services using jax-ws but the first surprise i got was
I´m trying to run an old .NET application from an ASP.NET website. After reading

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.