Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
Can we have in Java one byte whose upper 4 bits represent values like 0x40/0x80 and lower 4 bits representing values like 0,1,2,3.If yes then how do we retrieve values out of that on byte?Any help is greatly appreciated.
java bit manipulation
A simple example is probably easier than using words to describe it.
byte data = 0x74; int high4 = (data >> 4) & 0xf; int low4 = data & 0xf;
Add a comment
You can create wrapper class for byte or int with methods that fidget bits.
byte
int
int first4bits = (byteContainer >> 4) & 0xF; int last4bits = byteContainer & 0xF;
The problem is that such actions are inappropriate in Java.
Required, but never shown
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.
Explore related questions
See similar questions with these tags.
java bit manipulation