11 Replies Latest reply: Jan 16, 2008 10:43 AM by 807603

# bit manipulation - converting int value to 4 bits

Is there someway of converting int values to a set number of bits without affecting the other bits in the byte.
If I have a byte and I the first 3 bits are reserved and the other 5 bits represent a number.

So I need convert a number to the 5 bits without changing the values in the first 3 bits.

It could also be that I have 2 bytes and 12 bits are for a number and 4 are reserved.

I have been looking for answers but cannot find a solution.

Kelvin
• ###### 1. Re: bit manipulation - converting int value to 4 bits
???

Can you give an example of what you're trying to do?
• ###### 2. Re: bit manipulation - converting int value to 4 bits
Simple

1. Mask out the bits you need to change
2. "OR" in the new bits.
• ###### 3. Re: bit manipulation - converting int value to 4 bits
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/op3.html

~
• ###### 4. Re: bit manipulation - converting int value to 4 bits
What I am trying to do that I receive a byte of information from a stream, and then manipulate some parts of the information and then resend the byte array.

But basically if I have a byte that looks like 11100000.
If the first 3 bits are reserved and the last 5 bits represent a number. In this example the number will be 0.
I want to be able to insert the number e.g. 7 into the 5 bits, so that the result will be

11100111

Another example is that in the byte the 3rd and 4th (from left) bit represent a number
10101111 In this example the number is 2. I would then like to change in to 3.
10111111

Kelvin
• ###### 5. Re: bit manipulation - converting int value to 4 bits
Then see replies 2 & 3.
• ###### 6. Re: bit manipulation - converting int value to 4 bits
stoffin wrote:
But basically if I have a byte that looks like 11100000.
If the first 3 bits are reserved and the last 5 bits represent a number. In this example the number will be 0.
I want to be able to insert the number e.g. 7 into the 5 bits, so that the result will be

11100111

Another example is that in the byte the 3rd and 4th (from left) bit represent a number
10101111 In this example the number is 2. I would then like to change in to 3.
10111111

Kelvin
First off, I should mention that "first 3 bits" is generally interpreted to be the lower 3 bits, not the upper 3 bits. It's an ambiguous term, so stick to "upper" and "lower", or bit indexes (in this case, bits 5-7).

And as mentioned before, mask out the old bits, then OR in the new bits. If you don't know what this means, look at yawmark's link, and check out wikipedia.
• ###### 7. Re: bit manipulation - converting int value to 4 bits

I just came across this link which explains it all.

http://www.vipan.com/htdocs/bitwisehelp.html
• ###### 8. Re: bit manipulation - converting int value to 4 bits
I just came across this link which explains it all.
http://www.javaranch.com/campfire/StoryBits.jsp

~
• ###### 9. Re: bit manipulation - converting int value to 4 bits
yawmark wrote:
I just came across this link which explains it all.
http://www.javaranch.com/campfire/StoryBits.jsp
Yeesh! Yet another reminder as to why I can't stand to visit that site.
• ###### 10. Re: bit manipulation - converting int value to 4 bits
Yeesh! Yet another reminder as to why I can't stand to visit that site.
To each their own. The information contained in the site is correct. Different people have different learning styles, and I'd hate to see someone who might find it useful not visit the site based on commentary such as that.

~
• ###### 11. Re: bit manipulation - converting int value to 4 bits
yawmark wrote:
Yeesh! Yet another reminder as to why I can't stand to visit that site.
To each their own. The information contained in the site is correct. Different people have different learning styles, and I'd hate to see someone who might find it useful not visit the site based on commentary such as that.
Sorry, I didn't mean to discourage anyone. The site just rubs me the wrong way :-)

As you said, to each their own.