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

The Archive Base Latest Questions

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

I am trying to optimize my mysql table a little bit in order to

  • 0

I am trying to optimize my mysql table a little bit in order to have a slightly more manageable table. I would like to store user permissions in a bit field.

For example, a users permissions could be 0110 (I have a growing number of user permissions, so the length of this could be a bit longer)

The example might correspond to the following:

0: User cannot post news items on the website
1: User can post new articles on the website
1: User can edit any article on the website
0: User cannot remove articles from the website
etc. (for other permissions)

If I have this stored in a mysql bit field. How can I manipulate this in PHP?

For example, in PHP is there an easy way to get the 3rd bit and see if it is a 0 or a 1?

Is there an easy way to set the 3rd bit to be a 0 or a 1?

  • 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-13T07:11:05+00:00Added an answer on May 13, 2026 at 7:11 am

    You can use bitwise operators.

    For instance :

    $bits = bindec('0110');
    var_dump($bits & bindec('0100'));
    

    Will get you “4” (i.e. non-zero — which you can consider as “ok”), as the third bit in $bits is set.

    And :

    $bits = bindec('0010');
    var_dump($bits & bindec('0100'));
    

    Will get you 0 — as the third bit in $bits is not set.

    Of course, no need to call bindec each time — I just used it here to make things easier to read ; my first example could be rewritten as something like this :

    $bits = bindec('0110');
    var_dump($bits & 1<<2);
    

    And to make to code easier to understand, don’t use such magic values, but use some constants ; for instance :

    define('PERMISSION_TO_DO_X', 1);
    define('PERMISSION_TO_DO_Y', 1<<1);
    define('PERMISSION_TO_DO_Z', 1<<2);
    

    And to use them :

    $bits = bindec('0110');
    if ($bits & PERMISSION_TO_DO_X) {
        echo "OK to do X<br />";
    }
    if ($bits & PERMISSION_TO_DO_Y) {
        echo "OK to do Y<br />";
    }
    if ($bits & PERMISSION_TO_DO_Z) {
        echo "OK to do Z<br />";
    }
    

    Which, here, gets you that kind of output :

    OK to do Y
    OK to do Z
    

    Because :

    • the right-most bit is 0 (permission X not granted)
    • the second bit (starting from the right) is 1 : permission Y is granted
    • the third bit (still and always starting from the right) is 1 : permission Z granted
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to optimize a SQL Server. I have some experience with Mysql and
I'm trying to optimize query performance and have had to resort to using optimizer
I have a mysql table with over 4 million of data; well the problem
I've trying to optimize a query on MySQL that takes around 15-20 seconds to
I'm trying to optimize a query which uses a view in MySQL 5.1. It
I am trying to optimize a sql query which is using order by clause.
I have in my MySQL database these two tables: CREATE TABLE IF NOT EXISTS
Having some problems while trying to optimize my SQL. I got 2 tables like
I have been trying to optimize the two following nested loops: def startbars(query_name, commodity_name):
I am trying to optimize the following modified MySQL query from OsCommerce: select distinct

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.