Yes, we're now running our Black Friday Sale. All Access and Pro are 33% off until 2nd December, 2025:
How to Set a Specific Bit of a Number in Java
Last updated: November 18, 2025
1. Overview
There may be several cases where we need to set specific bits of an integer. For example, we use the individual bits of an integer mask when handling user permissions, such as read or write permissions. Another example is low-level programming, where we often need to set the bits of a register.
In this tutorial, we’ll discuss how to set a single bit or multiple bits of a number with an integral type using Java.
2. Setting a Single Bit
We can use the bitwise left shift operator (<<) together with the bitwise OR operator (|) to set a specific bit of an integer in Java. Let number be a variable of type int. The bit at index bitPosition can be set to 1 as follows:
number = number | (1 << bitPosition);
We start indexing from 0; i.e., the least significant bit of number corresponds to bitPosition equal to 0. We can think of the expression (1 << bitPosition) as a mask. It’s an integer whose bit at bitPosition is 1 and all other bits are 0.
When we merge number with (1 << bitPosition) using the bitwise OR operator, we set the bit at bitPosition to 1 while leaving other bits unchanged. Therefore, if the bit at bitPosition is 1, this operation has no effect.
We can further shorten the expression above by combining the assignment and bitwise OR operators using the bitwise OR assignment operator, |=, as follows:
number |= (1 << bitPosition);
Here is a simple example that sets the bit at index 2:
int number = 0;
int bitPosition = 2;
number |= (1 << bitPosition); // 4 (Binary: 100)
Initially, the value of number is 0. After setting the bit at index 2, its binary representation becomes 100, which is 4 in decimal.
If we set the bit at index 31 (the leftmost bit), the number becomes negative if it’s positive. This is because the leftmost bit is the sign bit.
Although the example above sets a specific bit in an int variable, we can use the same approach for any integral type, such as byte, short, or long.
3. Setting Multiple Bits
We can generalize the method in the previous section to set multiple bits of a number at once. For example, to set the bits at indexes 0, 2, and 3, we can use the following code snippet:
int number = 0;
int mask = (1 << 0) | (1 << 2) | (1 << 3);
number |= mask; // 13 (Binary: 1101)
The mask consists of three parts, and we create the mask by combining them using bitwise OR:
- (1 << 0) -> 0001
- (1 << 2) -> 0100
- (1 << 3) -> 1000
- mask   -> 1101
We apply the mask to the number again using the bitwise OR operator. number, which is originally 0, becomes 13 after applying the mask.
This method can also be applied to other integral types besides int.
We can also use these methods in other programming languages. For example, we can use the same code as is in C++.
4. Conclusion
In this article, we discussed how to set a single bit or multiple bits of an integral type in Java. Essentially, we created a bitmask with 1s in all the positions we want to set, using the bitwise left-shift operator. Then, we combined this mask with the number, whose corresponding bits are to be set, using the bitwise OR operator.
















