Write an Arduino statement that will cause bits 5 3 1 to be

Write an Arduino statement that will cause bits 5, 3, & 1 to be clear in the following. Write an Arduino statement that will test if bit 13 is set in the integer xyzzy.

Solution

For the first part of the Question:

Given input,

byte value = 0xFF

(Assumption: Least Significant (rightmost) Bit (LSB) is considered as Bit 0.)

Here we need to clear bits 5, 3, and 1 and remaining bits should not be altered.

Take a temporary byte value with bits 5, 3, and 1 as zeros and remaining bits as ones.

byte temp = 0xD5 ( equivalent binary value is 1101 0101)

So the ‘if’ condition can be written as:

If(           value & temp){                 // Here, result will be true if result is 1, else condition will be false.

}

For the second part of the Question:

This can be solved in two different ways -

1. By performing Bitwise AND operator (&) on the given input value with 0x000D (equivalent decimal value is 13).

Here,

Int xyzzy = 0xBEEF

Int tmp = 0x2000 (only bit 13 is 1)

If ( xyzzy & tmp){   // The condition will be satisfied only when bit 13 of xyzzy is set

}

2. By using Right shift operator (>>) on the given input by (bit -1) number of times and ANDing the result with 0x01. Here we don’t need to worry about LHS bits after bit position 13.

Here,

Int xyzzy = 0xBEEF

Int tmp = 0x000C (we have to test bit 13, so take tmp value as 12)

Int result = xyzzy >> tmp;

If ( result & 0x01){   // The result will be 1 only when bit 13 is 1, else 0.

}

Note : The above Questions can be answered using Arduino functions like bit, bitClear, and bitWrite. But, these functions need to be applied only on one bit at a time.

 Write an Arduino statement that will cause bits 5, 3, & 1 to be clear in the following. Write an Arduino statement that will test if bit 13 is set in the i

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site