10

I have some data in field type Byte ( I save eight inputs in Byte, every bit is one input ). How to change just one input in that field ( Byte) but not to lose information about others ( example change seventh bit to one, or change sixth bit to zero )?

3
  • Better use the primitive type byte. The wrapper class Byte should only be used where needed, e.g. when you want to put bytes into collections. Commented Jan 30, 2011 at 18:13
  • Sounds like you'd be better off using a set than an integer Commented Jan 30, 2011 at 18:59
  • possible duplicate of Set specific bit in byte Commented Jan 30, 2011 at 19:29

5 Answers 5

30

To set the seventh bit to 1:

b = (byte) (b | (1 << 6));

To set the sixth bit to zero:

b = (byte) (b & ~(1 << 5));

(The bit positions are effectively 0-based, so that's why the "seventh bit" maps to 1 << 6 instead of 1 << 7.)

Sign up to request clarification or add additional context in comments.

1 Comment

(This takes advantage of auto-boxing and -unboxing to convert from Byte to byte and back. If you're running in an older JVM, you'll have to convert them explicitly.)
15

Declare b as the primitive type byte:

byte b = ...;

Then you can use the compound assignment operators that combine binary operations and assignment (this doesn't work on Byte):

b |= (1 << bitIndex); // set a bit to 1
b &= ~(1 << bitIndex); // set a bit to 0

Without the assignment operator you would need a cast, because the result of the | and & operations is an int:

b = (byte) (b | (1 << bitIndex));
b = (byte) (b & ~(1 << bitIndex));

The cast is implicit in the compound assignment operators, see the Java Language Specification.

3 Comments

Performance-wise, is it true to say that the compound assignment one and the non-compound one is identical ?
Yes, most likely they compile to the same machine code. But I haven't checked, why don't you do that?
well I'm not even sure what should I Google to learn how to do that.
2

To set a bit use :

public final static byte setBit(byte _byte,int bitPosition,boolean bitValue)
{
    if (bitValue)
        return (byte) (_byte | (1 << bitPosition));
    return (byte) (_byte & ~(1 << bitPosition));
}

To get a bit value use :

public final static Boolean getBit(byte _byte, int bitPosition)
{
    return (_byte & (1 << bitPosition)) != 0;
}

Comments

0

Note that the "Byte" wrapper class is immutable, and you will need to work with "byte".

Comments

0

You really owe it to yourself to look into masking functions for and, or, and xor -- they allow you to simultaneously verify, validate, or change... one, some, or all of the bits in a byte structure in a single statement.

I'm not a java programmer by trade, but it's derived from C and a quick search online seemed to reveal support for those bitwise operations.

See this Wikipedia article for more information about this technique.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.