Clean app update code by storing 2 booleans into 1 int Android

Nilesh Deokar
MindOrks
Published in
4 min readJun 7, 2018

Every app developer out there must have faced this situation where they want to store two values of same type. Typical example is managing the app updates where you want to check whether update is available or not and if it is available then is it a mandatory update? Based on these values we often decide whether to allow user to continue using the app or force users to update. Simple solution is to declare two booleans, one with isUpdateAvailable and another with isMandatoryUpdate. What if we could merge these two boolean variables into one Integer. We could do this by using every bit values of an integer as boolean and to do this we need to know following thing :

Pic credits : anysoftwaretools.com

How to set a bit at specific position in Int ?

By set, I mean making a bit value 1. We can achieve this using BitSet class but it would be lot of new information so we will go ahead with simple bitwise logical operations.
Suppose we have int myOriginalValue = 0b0010; we want to set 3rd bit to 1

STEPS :

  1. For setting a bit value to 1 we will take the binary value of 1 and left shift it by position times which means we will left shift 1 by 3 times.
    0b0001 << 3 = 0b1000
  2. Now we will OR above output against myOriginalValue :
Demonstration of how to set a bit

Reference :

  1. How to Set a specific bit in byte
  2. How does the Bitwise Shifting works in Java
  3. Java BitSet example

Now let’s move to how we can utilise this in our use case.

Using every bit of an Int as boolean field :

Idea is to use the bits of an int and assign every bit for some purpose. In our above mentioned update app example we will use 2 bits, 1 for isUpdateAvailable and another for isMandatoryUpdate. Let’s assign bit positions 1 and 2 to them respectively. Now assume app is started and called splash webservice and send current version of an app in request body, server response says there is an update available So we need to set the isUpdateAvailable to True. This can be done on int variable in following way :

Setting bit values of an integer Java

After executing above code we have set the bit position 1 of mUpdateValue to true. Look at the following snippet to see how we can check the value of bit position 1 (UPDATE_AVAILABLE).

Getting bit values of an integer Java
  • STEP 1 : Will perform BitwiseLeftShift on 1 UPDATE_AVAILABLE time. which will be 1 << 1. In binary 1 = 0001 so after performing << 1 time 0001 will become 0010.
  • STEP 2 : Will do Bitwise AND on mUpdateValue and output of STEP 1.
    0010 & 0010 will result into 0010.
  • RESULT : Output of STEP 2 is not equal to 0 so the value of bit position 1(UPDATE_AVAILABLE) is True.

Below is the complete example for setting both bit poitions true and checking their values back :

Conclusion :

Imagine you’re working on the health app where in you want to store the medical history of a patient like weather patient ever had Malaria, Asthma, Tyroid and blah blah blah. This will result in adding a column in database for every disease and you would store 0 or 1 in it as SQLite does’t support boolean values. With this approach you can store upto 32 diseases into one integer column and could avoid creating a column per disease.

Pros :

  • Combines multiple fields.
  • Saves no of columns in db yielding db read and write performance.
  • Simple one line code for checking true or false.

Cons :

  • Every field has to be assigned with static bit position hence increased declaration of the static fields.

I have made an simple demo health app with above mentioned scenario source code can be found here :

Credits :

This idea was originally implemented by my colleague Shain Singh. I just tried to put it in words here. And Thanks to Dharmesh Sojitra for helping in technical editing.

Further Reading :

Why not more than 32 fields ?

Because we are using int, size of which is 32 bits means 32 possbile 1 and 0s. So when you try to do 1 << 33 times you get 0b0010 which is same as the 1 << 1 time.

What if we have more than 32 fields ?

Even if we are restricted by the size of an int to be 32 we are not restricted to data type int, we can change it to long and make it capable of 64 fields. Since 1 long = 64 bits.

What if we have more than 64 fields?

Now if we are going this big then it’s even better to consider using long array. We can make one long[] with desired size and then store the 0 to 63 fields in long[0], 64 to 127 fields into long[1] and so on.

Feel free to discuss, critique, and share.

My LinkedIn, Twitter. Let’s connect. :)

Check out all the top articles at blog.mindorks.com

--

--

Nilesh Deokar
MindOrks

Android Associate | Geek-ish | Crypt0gr4phy | UX Enthu