In this article, we will look at how to implement a low-level bit mask using bit operators. We will see how we can use a single int variable as a separate data container. Bitmask Bitmask allows us to store multiple values ​​in a digital variable. We no longer regard this variable

2025/06/1206:33:35 technology 1123

In this article, we will take a look at how to implement a low-level bit mask using bit operators. We will see how we can use a single int variable as a separate data container.

In this article, we will look at how to implement a low-level bit mask using bit operators. We will see how we can use a single int variable as a separate data container. Bitmask Bitmask allows us to store multiple values ​​in a digital variable. We no longer regard this variable - DayDayNews

-bit mask

-bit mask allows us to store multiple values ​​in a numeric variable. We no longer regard this variable as an integer, but we regard each of its bits as an independent value.

Because a bit can be equal to 0 or 1, we can also regard it as false or true. We can also cut a set of bits and treat them as a smaller numeric variable or even an String.

As an example

Assume we have a minimum memory space and need to store all information about the user account in a int variable. The first eight digits (from 32 available digits) will store boolean information, such as "Is the account activated?" or "Is the account premium?"

As for the remaining 24 digits, we will convert them into three characters as the user's identifier.

encoding

Our user will have an identifier "AAA" and he will have an active premium account (stored in the first two bits). In the binary representation, it will look like.

String stringRepresentation = "010000010100000101000001000011";

Unteger#parseUnsignedInt variable can be easily encoded as an int variable.

int intRepresentation = Integer.parseUnsignedInt(stringRepresentation, 2);assertEquals(intRepresentation, 1094795523);

decoding

This process can also be reversed using the Integer#toBinaryString method.

String binaryString = Integer.toBinaryString(intRepresentation);String stringRepresentation = padWithZeros(binaryString);assertEquals(stringRepresentation, "01000001010000010100000100001000011");

Extract one bit

First bit

If we want to check the first bit of our account variable, we only need to use the rank and operator and the number 1 as the mask. Because the number 1 is only the first bit set to 1 in binary form, and the rest are 0, it will remove all bits from our variable, leaving only the first full bit.

100000101000001010000001000000011000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001

Then we need to check whether the generated value is not equal to zero.

intRepresentation & 1 != 0

Bits at any position

If we want to check other bits, we need to create an appropriate mask. This mask needs to have a bit set to 1 at the given position and the rest set to 0. The easiest way is to shift the mask we already have.

1 (position - 1)

The position variable of the above line of code is set to 3, which will turn our mask from 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

10000010100000100001000001100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 

private boolean extractValueAtPosition(int intRepresentation, int position) {return ((intRepresentation) & (1 (position - 1))) != 0;}

To achieve the same effect, we can also move the intRepresentation variable in the opposite direction instead of changing the mask.

Extract multiple bits

We can extract multiple bits from an integer in a similar way. Let's extract the last three bytes of our user account variable and convert it into an string . First, we need to get rid of the influence of the first eight by moving the variable to the right.

int lastThreeBites = intRepresentation 8;String stringRepresentation = getStringRepresentation(lastThreeBites);assertEquals(stringRepresentation, "000000000100000101000001");

We still have 32 bits because int always has 32 bits. However, now we are only interested in the top 24 bits, and the rest are zero, which will easily be ignored. The int variable we created can be easily used as an integer ID, but since we want to have a string ID, we have one more step to do.

We will divide the binary string representation into 8-character groups, parse them into char variables, and then concatenate them into a final String.

For convenience, we will also ignore null bytes.

Arrays.stream(stringRepresentation.split("(?=\\G.{8})")).filter(eightBits - !eightBits.equals("00000000")).map(eightBits - (char)Integer.parseInt(eightBits, 2)).collect(StringBuilder::new, StringBuilder::append, StringBuilder::append).toString();

Apply a bit mask

We can also create a mask to check many bits at the same time instead of extracting and checking the values ​​of a single bit. We want to check if our user has an active premium account, so the first two bits of his variable are set to 1.

We can check them separately with the previous method, but creating a mask to select them will be faster.

int user = Integer.parseUnsignedInt("00000000010000101000010100001", 2);int mask = Integer.parseUnsignedInt("000000000000000000000000000000000000000000000011", 2);int masked = user & mask;

Because our user has an active account, but it is not a premium account, the masked value will only be set to 1.

assertEquals(getStringRepresentation(masked), "0000000000000000000000000000000000000000000000000000001");

Now we can easily and inexpensively assert whether a user meets our conditions.

assertFalse((user & mask) == mask);

Summary

In this tutorial, we learned how to use the bit operator to create a bitmask and apply it

technology Category Latest News