Check if byte has more than one bit set

valsidalv

I'm receiving a 3-byte message back from a serial device when I send it a query in my C# application. The entire message should have at most one bit set, e.g. $04 00 00 is fine, but not $04 01 00 or $05 00 00.

Bit-shifting and checking each bit is one solution but perhaps there is something more elegant? We can check each byte to see if it's a power of two but I can't come up with a way which looks nicer (not sure about efficiency in these cases either).

As an extension to the problem, I need to match each possible value to an output message, e.g.:
$01 00 00: Button A pressed
$00 02 00: Lever B pulled
$00 00 04: Switch C activated

A case statement is a simple solution, but once again, wondering if there's a pretty way of doing this as well.

justanothercoder

There is one clever trick to check if byte has more one bit set. n & (n - 1) != 0

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

check if the text has more than one link

From Dev

has_one relation automatically set nil if more than one

From Dev

has_one relation automatically set nil if more than one

From Dev

Quick way to check if a NodeList has more than one node?

From Dev

java Validations to check if more than one textbox has values

From Dev

EEPROM - writing/reading more than one byte

From Dev

Byte string spanning more than one line

From Dev

Changing more than one bit in a register

From Dev

How to determine if more than one bit in an STD_LOGIC_VECTOR is set in VHDL

From Dev

Check if more than 30 minutes has passed

From Dev

how to check if xml file has more than one child values using php

From Dev

Check if a shell variable has more than one line using built-ins?

From Dev

Which one-bit integer has more significant bit?

From Dev

Detect if a div has more than one line

From Dev

Detect if a div has more than one line

From Dev

Matching has more than one value

From Dev

Match middle minimum value in set that has more than one minimum value

From Dev

How do I set "all windows" by default when left clicking on a application that has more than one window?

From Dev

Check if text contains more than one @

From Dev

Android permission check in more than one function

From Dev

XOR encryption/decryption when the key is more than one byte long?

From Dev

more than one entry saved on has_one association rails

From Dev

Query to check if record has existed for more than X hours

From Dev

Check if drop-down has more than 1 option

From Dev

PHP: How to check if a number has more than two decimals

From Dev

Efficient way to check if a traversable has more than 1 element in Scala

From Dev

SQLite: fastest way to check if a table has more than x rows

From Dev

How to check if a folder has more than 20 files in it in batch?

From Dev

How to check in if my file has executable bit permission set in repo

Related Related

  1. 1

    check if the text has more than one link

  2. 2

    has_one relation automatically set nil if more than one

  3. 3

    has_one relation automatically set nil if more than one

  4. 4

    Quick way to check if a NodeList has more than one node?

  5. 5

    java Validations to check if more than one textbox has values

  6. 6

    EEPROM - writing/reading more than one byte

  7. 7

    Byte string spanning more than one line

  8. 8

    Changing more than one bit in a register

  9. 9

    How to determine if more than one bit in an STD_LOGIC_VECTOR is set in VHDL

  10. 10

    Check if more than 30 minutes has passed

  11. 11

    how to check if xml file has more than one child values using php

  12. 12

    Check if a shell variable has more than one line using built-ins?

  13. 13

    Which one-bit integer has more significant bit?

  14. 14

    Detect if a div has more than one line

  15. 15

    Detect if a div has more than one line

  16. 16

    Matching has more than one value

  17. 17

    Match middle minimum value in set that has more than one minimum value

  18. 18

    How do I set "all windows" by default when left clicking on a application that has more than one window?

  19. 19

    Check if text contains more than one @

  20. 20

    Android permission check in more than one function

  21. 21

    XOR encryption/decryption when the key is more than one byte long?

  22. 22

    more than one entry saved on has_one association rails

  23. 23

    Query to check if record has existed for more than X hours

  24. 24

    Check if drop-down has more than 1 option

  25. 25

    PHP: How to check if a number has more than two decimals

  26. 26

    Efficient way to check if a traversable has more than 1 element in Scala

  27. 27

    SQLite: fastest way to check if a table has more than x rows

  28. 28

    How to check if a folder has more than 20 files in it in batch?

  29. 29

    How to check in if my file has executable bit permission set in repo

HotTag

Archive