Skip to content

nieldeokar/HealthApp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation


HealthApp using BitwiseShifting

This demo app shows how to use 1 Integer value to store upto 32 booleans. Find the reference article here.

Using Bit values

Idea is to utilise every bit inside an Integer for boolean representation. 1 Integer = 32 bits so we have 32 possible variables which coulde be either 1 or 0. By setting bit value of given position we will make it 1 which is True and by resetting we will make bit value at that position to 0 which is false.

Preview

demo image

How to set bit Value to TRUE :

public class SetAppUpdateAvailable {

    private int mUpdateValue = 0;

    private static final int UPDATE_AVAILABLE = 1;
    private static final int UPDATE_COMPULSORY = 2;

    public void setUpdateAvailable(){
        mUpdateValue = mUpdateValue | (1 << UPDATE_AVAILABLE);
    }
}

How to get bit value :

public class GetAppUpdateAvailable {

    private int mUpdateValue = 0b0010;
    
    private static final int UPDATE_AVAILABLE = 1;
    private static final int UPDATE_COMPULSORY = 2;

    public void getUpdateAvailable(){
        boolean result = (mUpdateValue & (1 << UPDATE_AVAILABLE)) != 0;
    }
}

This way we can store upto 32 boolean values into 1 integer.

Want to store more than 32 boolean values ?

In this case you can change variable type to Long which will give you 64 bits. You need more than that ? Then consider using long[] of size n It should work for you.

  1. SingleIntBitmaskHandler
  2. SingleLongBitmaskHandler
  3. MutlipleLongBitmaskHandler

About

This demo app shows how to store up to 32 booleans in one Integer.

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors