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

The Archive Base Latest Questions

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

In some places, you’ll see options saved as numbers. For example, when setting file

  • 0

In some places, you’ll see options saved as numbers. For example, when setting file permissions, you pass them using a value ranging from 0 to 7 for each group of users.
Each one of the bits in the binary representation of the number represents one of three permissions: read, write and execute, so a value of 7, with a binary representation of 111 means that the file can be read, written and executed, whereas a value of 5 means that the file can only be read and executed because of the binary representation 101.

side note: I’m not an expert on how operating systems manage files; my explanation may be wrong, but I’m sure that the basic idea of binary numbers representing these options is implemented there.

I am now wondering if it is efficient to store true/false options in binary using JavaScript. To do so, I would create the JavaScript as follows:

function OptionsGenerator(){
    //this method takes a number, and returns an array of options.
    this.num2options = function(num){
        var str,len,opts=[];
        if(typeof num !== "number") return false;
        str = num.toString(2);
        len = str.length;
        for(var i=0;i<len;i++){
            opts[i] = (str.charAt(i)==="1") ? true : false;
        }
        return opts;
    }
    //this function gets an array of options, and returns a number.
    this.options2num = function(opts){
        var str = "",o,opt;
        for(o in options){
            opt = (options[o]) ? 1 : 0;
            str+=opt;
        }
        return parseInt(str,2);
    }
    //this function returns a specific option with an index ranging from 1 to the number of options set. takes 2 arguments: the first one is the set of options. This can either be a number or a string containing a binary representation of the number, the second parameter contains the index of the option.
    this.getOption = function(num,optnum){
        var str;
        if(typeof num === 'number')
            str = num.toString(2);
        else
            str = num;
       return (num.charAt(optnum-1)==="1");
    }
}

My question is: is this an efficient way to save options, and if it is, is there a more efficient way to convert from binary to a number, and to get a specific option, or all the options?

  • 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-22T22:29:50+00:00Added an answer on May 22, 2026 at 10:29 pm

    If you are wanting to accomplish something similar in JavaScript, I would recommend actually using bitwise operators, namely & and |.

    For example:

    var read = 1;
    var write = 2;
    var execute = 4;
    
    var rw = read | write;
    
    var someUserPermission = 3;  // or, var someUserPermission = read & write;
    
    // can read? 
    console.log((someUserPermission & read) === read); // => true
    console.log((someUserPermission & write) === write); // => true
    console.log((someUserPermission & execute) === execute); // => false
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have developed a GUI using ASP.NET 3.5 and C#.... Now at some places
In some places I see that IsInitiating creates a new session. Other places say
Perusing the code I am maintaining, I see that in some places the With
(Basically we want to import targets files at some places in the *.csproj file
I have seen in some places that while using Expandable List View we register
In my jquery snippet, am using the function(e) and also in some places i
I am writing an embedded app. In some places, I use std::ostringstream a lot,
I'm working on a class-based php web app. I have some places where objects
I was trying to place some simple effects on an image using HBox using
At some places in my code, I print debug messages like this: int ret

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.