Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

 Storing multiple boolean in an integer

views
     
TSnarf03
post Oct 24 2021, 01:12 AM, updated 3y ago

Look at all my stars!!
*******
Senior Member
4,545 posts

Joined: Dec 2004
From: Metro Prima, Kuala Lumpur, Malaysia, Earth, Sol


I think this problem already well known in bit shifting, but im not using bit, need solution in actual number(s)

Lets say i have multiple boolean data(b0-bn) can be either true or false, and i need to store them in an integer(name it T)

b0 with value of 1
b1 with value of 2
b2 with value of 4
b3 with value of 8
b4 with value of 16
...

so if all of them are true, then T will have value of 1+2+4+8+16 = 31, if b0 is false then T will have value of 30(2+4+8+16).

now, i need to have a very fast and efficient method to know if certain boolean data is true or false without evaluating from the start or from the end.

like i need a formula to know if b4 is true or false if T is 4923849235649 (assume it has far more than just b0-b4)

please do not offer any bitwise solution.
FlierMate
post Oct 24 2021, 02:18 AM

On my way
****
Validating
543 posts

Joined: Nov 2020
Interesting question for not using bit shifting.

Here is my own code, you can always use Math.Pow (2, power) in substitution of bit shifting.

CODE
using System;
   
public class Program
{
public static bool GetBool(int num, int slot)
{
 return (bool)((num & (int)Math.Pow(2,(double)slot-1))>0);
}

public static void Main()
{
   
 Console.WriteLine(GetBool(15,1));//0 1111 - bingo
 Console.WriteLine(GetBool(15,2));//0 1111 - bingo
 Console.WriteLine(GetBool(15,3));//0 1111 - bingo
 Console.WriteLine(GetBool(15,4));//0 1111 - bingo
 Console.WriteLine(GetBool(15,5));
 
 Console.WriteLine(GetBool(16,1));
 Console.WriteLine(GetBool(16,2));
 Console.WriteLine(GetBool(16,3));
 Console.WriteLine(GetBool(16,4));
 Console.WriteLine(GetBool(16,5)); //1 0000 - bingo
}
}


Feel free to test more.
FlierMate
post Oct 24 2021, 02:40 AM

On my way
****
Validating
543 posts

Joined: Nov 2020
This is the code to reverse the process:

CODE
public static int SetBool(int num, int slot, bool state)
{
 int val=0;
 if (state) {
  val=num | (int)Math.Pow(2,(double)slot-1); }
 else {
  val=num & ~(int)Math.Pow(2,(double)slot-1); }
 return val;
}


And how to use it....

CODE
public static void Main()
{  
 int magic = 5;
 
 Console.WriteLine(GetBool(magic,1)); // 101 - true
 Console.WriteLine(GetBool(magic,2)); // 101 - false
 Console.WriteLine(GetBool(magic,3)); // 101 - true
 
 magic = SetBool(magic, 1, false);
 magic = SetBool(magic, 2, true);
 magic = SetBool(magic, 3, false);
 
 Console.WriteLine(GetBool(magic,1)); // 101 - false
 Console.WriteLine(GetBool(magic,2)); // 101 - true
 Console.WriteLine(GetBool(magic,3)); // 101 - false


It still use bitwise operator anyway..... rolleyes.gif
arturo_bandini
post Oct 24 2021, 09:31 AM

Getting Started
**
Junior Member
135 posts

Joined: Aug 2005


QUOTE(narf03 @ Oct 24 2021, 01:12 AM)
I think this problem already well known in bit shifting, but im not using bit, need solution in actual number(s)

Lets say i have multiple boolean data(b0-bn) can be either true or false, and i need to store them in an integer(name it T)

b0 with value of 1
b1 with value of 2
b2 with value of 4
b3 with value of 8
b4 with value of 16
...

so if all of them are true, then T will have value of 1+2+4+8+16 = 31, if b0 is false then T will have value of 30(2+4+8+16).

now, i need to have a very fast and efficient method to know if certain boolean data is true or false without evaluating from the start or from the end.

like i need a formula to know if b4 is true or false if T is 4923849235649 (assume it has far more than just b0-b4)

please do not offer any bitwise solution.
*
actually the "well-known" solution is not using bit shifting, but just simple bitwise AND (paired with bitwise OR when constructing the value).

i think bitwise AND is available almost everywhere, even in javascript - not sure why you would avoid anything "bitwise".

javascript reference on bitwise AND

basically, for your example:

QUOTE
Welcome to Node.js v14.15.4.
Type ".help" for more information.
> T = 4923849235649
4923849235649
> b4mask = 16
16
> b4flag = T & b4mask
0
>
so, in this case, b4 is false (0) for value T = 4923849235649

flashang
post Oct 24 2021, 10:40 AM

Casual
***
Junior Member
341 posts

Joined: Aug 2021


QUOTE(arturo_bandini @ Oct 24 2021, 09:31 AM)
actually the "well-known" solution is not using bit shifting, but just simple bitwise AND (paired with bitwise OR when constructing the value).

i think bitwise AND is available almost everywhere, even in javascript - not sure why you would avoid anything "bitwise".

basically, for your example:
so, in this case, b4 is false (0) for value T = 4923849235649
*
another example using bitwise :

CODE

const languages = {
python : 1,
c : 2,
java : 4,
cpp : 8,
csharp : 16,
vb : 32,
js : 64,
sql : 128,
php : 256,
asm : 512,
classicvb : 1024,
go : 2048,
matlab : 4096,
r : 8192,
groovy : 16384,
ruby : 32768,
swift : 65536,
fortran : 131072,
perl : 262144,
delphi : 524288
};

let yourChoice = languages.python + languages.java + languages.c;

console.log( yourChoice ); // 13


// any bit match
if( ( yourChoice & languages.java ) > 0 ) {
console.log( "Your choose Java." );
} else {
console.log( "Your didn't choose Java." );
}

// all bits match
if( ( yourChoice & (languages.java + languages.cpp) ) == (languages.java + languages.cpp) ) {
console.log( "Your choose Java AND cpp." );
} else {
console.log( "Your didn't choose both Java AND cpp." );
}

console.log( languages ); // show the list



smile.gif


FlierMate
post Oct 24 2021, 11:06 AM

On my way
****
Validating
543 posts

Joined: Nov 2020
AND is a generally accepted answer (for determining the value), OR is used to constructing the value. But I found that have to use AND NOT to construct value with FALSE boolean state. (OR is for constructing TRUE boolean state only).

One more note, please use uint (unsigned integer) if you use leftmost bit (e.g. 31th bit of a 32-bit integer), don't use int for bit manipulation, because bad things will happen (as the leftmost bit is for positive or negative sign).
TSnarf03
post Oct 24 2021, 12:38 PM

Look at all my stars!!
*******
Senior Member
4,545 posts

Joined: Dec 2004
From: Metro Prima, Kuala Lumpur, Malaysia, Earth, Sol


QUOTE(FlierMate @ Oct 24 2021, 02:18 AM)
Interesting question for not using bit shifting.

Here is my own code, you can always use Math.Pow (2, power) in substitution of bit shifting.

CODE
using System;
   
public class Program
{
public static bool GetBool(int num, int slot)
{
 return (bool)((num & (int)Math.Pow(2,(double)slot-1))>0);
}

public static void Main()
{
   
 Console.WriteLine(GetBool(15,1));//0 1111 - bingo
 Console.WriteLine(GetBool(15,2));//0 1111 - bingo
 Console.WriteLine(GetBool(15,3));//0 1111 - bingo
 Console.WriteLine(GetBool(15,4));//0 1111 - bingo
 Console.WriteLine(GetBool(15,5));
 
 Console.WriteLine(GetBool(16,1));
 Console.WriteLine(GetBool(16,2));
 Console.WriteLine(GetBool(16,3));
 Console.WriteLine(GetBool(16,4));
 Console.WriteLine(GetBool(16,5)); //1 0000 - bingo
}
}


Feel free to test more.
*
thanks for your idea(s), do u think this will work 2 ?
generally modified from yours

CODE


using System;

namespace ConsoleApp1 {
   class Program {
       static void Main(string[] args) {
           for (int i = 0; i < 65; i++) {
               Console.WriteLine(i + " " + fctCheckFlag(i, 4));
           }
       }

       public static bool fctCheckFlag(int intValue, int intFlag) { // flag = 1 2 4 8 16 32 64 128 256 512 1024
           return (bool)(intValue % (intFlag * 2) >= intFlag);
       }
   }
}



FlierMate
post Oct 24 2021, 02:00 PM

On my way
****
Validating
543 posts

Joined: Nov 2020
QUOTE(narf03 @ Oct 24 2021, 12:38 PM)
thanks for your idea(s), do u think this will work 2 ?
generally modified from yours

CODE


using System;

namespace ConsoleApp1 {
   class Program {
       static void Main(string[] args) {
           for (int i = 0; i < 65; i++) {
               Console.WriteLine(i + " " + fctCheckFlag(i, 4));
           }
       }

       public static bool fctCheckFlag(int intValue, int intFlag) { // flag = 1 2 4 8 16 32 64 128 256 512 1024
           return (bool)(intValue % (intFlag * 2) >= intFlag);
       }
   }
}

*
Yes, I ran a quick test,

CODE
 for (int i = 0; i < 128; i++) {
  // fctCheckFlag(i, 2) = GetBool(i,2)
  // fctCheckFlag(i, 4) = GetBool(i,3)
  // fctCheckFlag(i, 8) = GetBool(i,4)
  // fctCheckFlag(i,16) = GetBool(i,5)
              Console.WriteLine(i + " " + fctCheckFlag(i, 16).Equals(GetBool(i,5)));
          }


And your code actually works!!! Good that you found simpler way to achieve the same result. Thumbs up for you! thumbsup.gif
How did you do that?


rootbeer
post Oct 24 2021, 02:10 PM

Getting Started
**
Junior Member
210 posts

Joined: Dec 2006


Such usually use on what... Why don't. Use array key or object?
TSnarf03
post Oct 24 2021, 02:20 PM

Look at all my stars!!
*******
Senior Member
4,545 posts

Joined: Dec 2004
From: Metro Prima, Kuala Lumpur, Malaysia, Earth, Sol


QUOTE(rootbeer @ Oct 24 2021, 02:10 PM)
Such usually use on what...  Why don't. Use array key or object?
*
might be using it in unity burst compiler, its something very new to me, so i prefer not to use things that im not familiar with, like bitwise/shift =P

https://docs.unity3d.com/Packages/com.unity...nual/index.html
QUOTE
Burst does not support:
-DllImport or calli (This should be supported in a future version)
-catch
-try/finally (which will come at some point)
-foreach as it is requiring try/finally (This should be supported in a future version)
-Loading from non readonly static fields or storing to static fields
-Any methods related to managed objects (e.g array access...etc.)

 

Change to:
| Lo-Fi Version
0.0140sec    0.49    5 queries    GZIP Disabled
Time is now: 29th March 2024 - 07:48 PM