aspose file tools*
The moose likes Beginning Java and the fly likes Best approach for handling bits of a byte Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Best approach for handling bits of a byte" Watch "Best approach for handling bits of a byte" New topic
Author

Best approach for handling bits of a byte

Mike Bates
Ranch Hand

Joined: Sep 19, 2009
Posts: 81
I am reading data from a serial device and several of the fields are alarm fields where the bits on or off indicate the alarm is on or off at that moment.



What is the best approach for determining a bit is on or off and managing something like this in Java? So if bit 1 is on I need to indicate the High outside temp alarm went active. Is this something I would use a collection or ENUM for? Being still new to Java trying to figure out how to do it right or best.

Thanks
Mike
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18836
    
  40

IMO, the way to do this in Java is the same as C or Assembly -- use a bit mask to AND out rest of bits, and test for non-zero. Of course, this may be my C or Assembly background talking...

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

I'm with Henry. You define these bit masks as constants. For example:
Checking is then as easy as Henry said:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mike Bates
Ranch Hand

Joined: Sep 19, 2009
Posts: 81
Henry, Rob,

Thanks for the responses and the example. I have not done this kind of thing in C or Assembler. Twiddling bits has not been easy for me so I am learning as I go. I am Googling references on this type of approach to better understand what is going on.

Thanks again,

Mike
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18836
    
  40

Rob Prime wrote:


Rob,

I think the comparison operator has higher precedence than the bitwise AND. You'll need parens.

Henry
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
Henry Wong wrote:I think the comparison operator has higher precedence than the bitwise AND.
Look at this, which confirms Henry's suspicion.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

That's still my biggest weakness - I can never remember some of the precedence rules. Fortunately the compiler helps me out in those cases.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18836
    
  40

Rob Prime wrote:That's still my biggest weakness - I can never remember some of the precedence rules. Fortunately the compiler helps me out in those cases.


I don't even bother anymore... working in many languages simultaneously, some with slightly different precedence rules, it is just easier to add parens to everything.

And worse, while this would have been caught by the Java compiler, it wouldn't have by the C++ compiler.

Henry
Mike Bates
Ranch Hand

Joined: Sep 19, 2009
Posts: 81
Okay, just a bit further now, (got to love putting as much data into a byte as possible. I have two bytes that break down as follows:


Trying to outline the code a bit (pun not intended):



I think this is the right track. Any thoughts?

Thanks
Mike
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18836
    
  40

Should be...




Henry
Mike Bates
Ranch Hand

Joined: Sep 19, 2009
Posts: 81
Henry,

Thanks, I can't add. I am going to code this up and see what happens.

Mike
Mike Bates
Ranch Hand

Joined: Sep 19, 2009
Posts: 81
Well either the data I grabbed was wrong or I have something incorrect, probably both. I did find I had to worry about byte order, so the input was actually 0x89b7 and flipped the bytes to 0xb789. Right now the data run is showing 0xffff so waiting for some valid data to make sure I got it right. Trying to create a test format for today's date for testing.



Output:


Mike
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18836
    
  40

You forgot to shift the value into place. Remember only the year is at bit zero. The bit mask will get you the bits, but they are in the wrong location...



Henry
Mike Bates
Ranch Hand

Joined: Sep 19, 2009
Posts: 81
Henry,

That was it. Had to do the same for the month as well. Mask and shift.

Thanks
Mike
Ulrika Tingle
Ranch Hand

Joined: Nov 24, 2009
Posts: 92
Mike Bates wrote:
Twiddling bits has not been easy for me so I am learning as I go. I am Googling references on this type of approach to better understand what is going on.


Bit fiddling is described in The Java Programming Language by Gossling and others. You're not a real Java programmer until you have this book.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
Ulrika Tingle wrote: . . . You're not a real Java programmer until you have this book.
Bad spelling there. It should be

until you have read this book
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Best approach for handling bits of a byte