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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:05:49+00:00 2026-05-29T04:05:49+00:00

I have data stored in SQL as an integer between 0 and 63, IE

  • 0

I have data stored in SQL as an integer between 0 and 63, IE six bits.
I am trying to execute (or not) 6 simple statements based on respective bit values.

I want to know which method is the most efficient, or possibly if I’m making my life far harder than it has to be (which I suspect is true).

Method 1:

Loop working from smallest to largest. Divide the integer by 2, if not an integer then subtract .5 and the first bit is true. Repeat until the value is zero.

Method 2:

Convert to binary, pad with leading zeroes, then reading each character of the output string. (lots of functions and type conversions.)

    $bit = str_pad(decbin($row['utilities']),6,"0", STR_PAD_LEFT)

Method 3:

Loop working from largest to smallest. If the integer is greater than 32, then subtract 32 and the last bit is true. Divide test digit by 2 and repeat until testing for 0.5.

This is the code I’m actually working with. (Where $row[‘utilities’] is output from SQL and contains a number between 0 and 63 inclusive) The form element which creates the data for storage in the SQL database is identical except for the php.

    $bit = ???
    <select name="ut[]" multiple="multiple" class="postSelect">
        <option <?php if ($row['utilities']==0) {echo "selected='selected'";}?> value="0">None</option>
        <option <?php if (bit['0']) {echo "selected='selected'";}?> value="1">Electricity</option>
        <option <?php if (bit['1']) {echo "selected='selected'";}?> value="2">Water</option>
        <option <?php if (bit['2']) {echo "selected='selected'";}?> value="4">Heat</option>
        <option <?php if (bit['3']) {echo "selected='selected'";}?> value="8">Phone</option>
        <option <?php if (bit['4']) {echo "selected='selected'";}?> value="16">Internet</option>
        <option <?php if (bit['5']) {echo "selected='selected'";}?> value="32">Laundry</option>
    </select>
  • 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-29T04:05:50+00:00Added an answer on May 29, 2026 at 4:05 am

    My advice:
    For this use a bitwise operator instead of decbin and such. This makes it not only easier, but cleans up the code alot.

    Syntax:

    if($yourstoragebytes & $bittocheckfor) {
        // do something
    }
    

    Here’s a wonderful example taken from the php manual:

    $writePost = 1;
    $readPost = 2;
    $deletePost = 4;
    $addUser = 8;
    $deleteUser = 16;
    
    // User groups:
    $administrator = $writePost | $readPosts | $deletePosts | $addUser | $deleteUser;
    $moderator = $readPost | $deletePost | $deleteUser;
    $writer = $writePost | $readPost;
    $guest = $readPost;
    
    // function to check for permission
    function checkPermission($user, $permission) {
        if($user & $permission) {
            return true;
        } else {
            return false;
        }
    }
    
    // Now we apply all of this!
    if(checkPermission($administrator, $deleteUser)) {
        deleteUser("Some User"); # This is executed because $administrator can $deleteUser
    }
    

    You can do this in MySQL, too, but from my experience this usually makes your code longer and not as good as readable.

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

Sidebar

Related Questions

I have text stored in SQL as HTML. I'm not guaranteed that this data
I have data stored in a SQL database that I'm attempting to read into
I have address data stored in an sql server 2000 database, and I need
I have some XML data stored in a varchar(max) column on SQL Server 2005.
I have a simple Core Data store with an entity Cost with an integer
I have an application that sends data based on user interaction (not user input).
I have a C#.NET desktop application using SQL Compact edition as data store. The
I have a web service that stores data in a local SQL Server 2008
I have been spoiled by either using sql server to store data, or using
We have a WinForms application that uses SQL server to store its data. To

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.