• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Best approach for handling bits of a byte

 
Mike Bates
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21192
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm with Henry. You define these bit masks as constants. For example:
Checking is then as easy as Henry said:
 
Mike Bates
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21192
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:


Rob,

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

Henry
 
Campbell Ritchie
Sheriff
Pie
Posts: 49380
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21192
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21192
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should be...




Henry
 
Mike Bates
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry,

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

Mike
 
Mike Bates
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21192
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry,

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

Thanks
Mike
 
Ulrika Tingle
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49380
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic