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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:52:04+00:00 2026-05-26T23:52:04+00:00

How do I check whether a bit is 1 or 0 in PHP? I

  • 0

How do I check whether a bit is 1 or 0 in PHP?

I need to loop through a large file and check each bit, whether it is 1 or 0 and put this into a multi-dimentional array based on a set “column length”.

Something like:

$binary = 0100110110110001100010001111101100111;
$binary=binaryToArray($binary,10);

And the result would look like:

array[0][0]=0; array[0][1]=1; array[0][2]=0; //etc.
array[1][0]=1; array[1][1]=1; array[1][2]=0; //etc.
  • 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-05-26T23:52:05+00:00Added an answer on May 26, 2026 at 11:52 pm

    You can write a class that wraps the data and returns an instance of another class that wraps the byte:

    class DataBitAccessor implements ArrayAccess {
        private $data;
        public __construct($data) {
            $this->data = $data;
        }
    
        public offsetExists($offset) {
            return is_int($offset) &&
                    $offset >= 0 && $offset < strlen($this->data);
        }
        public offsetGet($offset) {
            if (!$this->offsetExists($offset)) {
                return null;
            }
            return new Byte($this->data{$offset});
        }
        public offsetSet($offset, $value) {
            throw new LogicException();
        }
        public offsetUnset($offset) {
            throw new LogicException();
        }
    }
    
    class Byte implements ArrayAccess {
        private $byte;
        public __construct($byte) { $this->byte = ord($byte); }
        public offsetExists($offset) {
            return is_int($offset) &&
                    $offset >= 0 && $offset < 8;
        }
        public offsetGet($offset) {
            if (!$this->offsetExists($offset)) {
                return null;
            }
            return ($this->byte >> $offset) & 0x1;
        }
        public offsetSet($offset, $value) {
            throw new LogicException();
        }
        public offsetUnset($offset) {
            throw new LogicException();
        }
    }
    

    (untested)

    You can also return a string directly from DataBitAccessor

    return str_pad(base_convert(
            ord($this->data[$offset]), 10, 2), 8, "0", STR_PAD_LEFT);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to check whether a page is being redirected or not without actually
I need to check whether the user executing the script has administrative privileges on
i need to check whether the user clicking the browser Refresh button and redirect
I want to check whether a string is a file name (name DOT ext)
How to check whether windows imaging component is installed or not on 64 bit
I wrote this sample code to check whether integer or string index is better
I need to find whether a new schedule overlaps any existing schedules. This is
I have a bit of code that is supposed to check whether a directory
I need to check whether certain Debian packages are installed on a system in
I want to I check whether a string is in ASCII or not. I

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.