Store Multiple Values in an Integer using JAVA

Store Multiple Values in an Integer using JAVA

There are many different ways to apply the concept of storing multiple values in an integer. Some of these are:

1. To store user privileges
2. To store a user’s profile
3. To store the current state of a player in a game

Of course there are other ways these things can be achieved but I quite like the simplicity that comes with using a single integer to store multiple values.

In this post, I will first explain the logic involved when storing multiple values in an integer. Then I will go on to give an example of how to store multiple values in an integer using Java.

The Logic

There are 2 steps involved in storing multiple values in an integer.

First, you need to assign each of the multiple values to an integer. If, for instance, you are trying to store a users profile, you would assign values like this:


	1   = is currently employed
	2   = is computer literate
	4   = has high school diploma
	8   = has degree
	16  = has children
	32  = is married
	64  = can drive
	128 = has passport
	256 = aged over 18

Notice that each integer is twice the size of the previous one. The reason for this is that these numbers when in binary representation only have one bit that is not 0, which is exactly what we need. The numbers above in binary are:


	1   = 00000001
	2   = 00000010
	4   = 00000100
	8   = 00001000
	16  = 00001000
	32  = 00010000
	64  = 00100000
	128 = 01000000
	256 = 10000000

The next step in storing multiple values in an integer is add all the binary representations of the values that are true for a user. So in this example, lets say a user has the following 4 characteristics:

1. Is currently employed
2. Is computer literate
3. Is married
4. Can drive

This means that the integer representation of each of these values is 1, 2, 32 and 64 respectively. Which we add:


1 + 2 + 32 + 64 = 99

So this particular users information is all stored in the number 99.

To retrieve the users information all we would do is convert the number 99 to binary. 99 in binary is 01100011. Because there are 4 1-bit values in this number, we know that there are 4 values stored in this integer, namely:


 00000001
 00000010
 00100000 
 01000000

We can get the integer equivalent for each of these binary numbers and use those to find the String value that each integer represents, thus giving us the full profile information.

Now lets see how we can code this in Java.

Java Example

I will use the previous example to show how to code this in java. First, declare integers that will be used to store a String value and set their values. For example,


int i = 1;
int a = 2;
int b = 4;
...

Then, use a Map to store all users names and the integers representing their information.


Map m = new HashMap();
	m.put(99, "Seda");

We then use the bitwise operator & to test the users integer against the declared integer. Read more about bitwise operators here. The full code for how to store multiple values in an integer in Java is shown below.

The code above outputs the following:


The profile for this user: Miriam
	Is currently employed
	Is over 18 years

The profile for this user: Billy
	Is currently employed
	Has drivers licence

The profile for this user: Seda
	Is currently employed
	Is computer literate
	Is married
	Has drivers licence

The profile for this user: Mwelwa
	Is currently employed
	Has a degree
	Has children
	Is married
	Has drivers licence

The profile for this user: Douglas
	Is currently employed
	Has a degree
	Is married
	Has drivers licence
	Is over 18 years

The profile for this user: Phyllis
	Is currently employed
	Is computer literate
	Has a degree
	Has children
Seda
Seda Kunda is a web designer and developer with a degree in Computer Science and a great passion for code. Besides code, she enjoys pepperoni pizza, watching the bachelor and sleeping in on Saturdays.
Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn