troubleshooting Question

Java bit inspection

Avatar of cgray1223
cgray1223 asked on
Java
5 Comments1 Solution467 ViewsLast Modified:
Hello,

I need a sanity check here...
Lets say II have a hexadecimal String of "D410" and I'm inspecting byte 1.  bits 1-4 is the lower nibble "4" and bits 5-8 is the upper nibble "D".  

1.  The most significant byte is 0 and the most significant bit is bit 7.  What exactly does that mean?
2.  If I had a requirement to get byte 1, bit 3's value (0 or 1), what is the best way to accomplish this?

I would attempt to do this using the following methodology:

-for bit 3 I would look at the "4" from the hex String of "D410" and parse that to an Integer with a radius of 16 and then call the readBit method passing it the bit location of 3.  I'm unsure if passing readBit the value 3 is the proper parameter?

//get the value of bit 3 of byte 1 of the below hex String
String hex="D410";
readBit(Integer.parseInt(hex.substring(1,2), 16), 3);

 public int readBit(Integer i, int bit) {
        int binaryValue = 0;
        if (i != null) {
            if ((i & (1 << bit)) != 0) {
                binaryValue = 1;
            }
        }
        return binaryValue;
    }


ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros