• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Best approach for handling bits of a byte

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23907
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Marshal
Posts: 22450
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23907
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 74034
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 22450
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23907
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23907
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Should be...




Henry
 
Mike Bates
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23907
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henry,

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

Thanks
Mike
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 74034
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
No holds barred. And no bars holed. Except this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic