0

I have found an Arduino function which converts a 2-bytes-long-value to a binary value.

e.g. I call sendCommand(0x05) And it gives me an output:

00000101

void sendCommand(unsigned long command){
 for (unsigned long mask = 1UL << (7); mask; mask >>= 1) {
 if (command & mask) {
 Serial.print( "1" );
 } else {
 Serial.print( "0" );
 }
 }
}

my Problem is, that I want to have the Least significant bit first. How do I have to adjust this function to achieve this?

e.g. sendCommand(0x05) shall give an output:

10100000

asked Dec 14, 2015 at 12:37

1 Answer 1

1
void sendCommand(unsigned long command){
 for (unsigned long mask = 1UL; mask<256UL; mask <<= 1) {
 if (command & mask) {
 Serial.print( "1" );
 } else {
 Serial.print( "0" );
 }
 }
}

PS a long is actually 4 bytes long. But your code only uses 1 byte of it.

answered Dec 14, 2015 at 13:12

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.