Pages: [1]   Go Down
Author Topic: Small help needed with #Define  (Read 516 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi everyone,

I am really new to using Arduinos, less than a week in fact, so my query is rather basic. Can someone explain what the following command is likely to do:

"#define BIT_IS_SET(i, bits)  (1 << i & bits)"

This is the first line of code being used to make an IR remote control, found at the following link:
http://www.zovirl.com/2008/11/12/building-a-universal-remote-with-an-arduino/

I'd be grateful for any help

Thanks

Viren
Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

"#define BIT_IS_SET(i, bits)  (1 << i & bits)"
Code:
1 << i

This means take the number 1 (the first "1"):
Code:
00000001
then "shifting" it right i bits. So if i was 4, 1<<i would be
Code:
00010000
See, the 1 is now 4 digits more to the left. This is exactly the same as saying
Code:
i * 16 // 16 is 2^4
The & is the bitwise AND. This means that it cycles through each bit in each byte, and if they are the same, it makes the corresponding resulting bit 1. Example:
Code:
01011001 &
11011101 =
01011001

Since 1<<i will be all zeros except for one digit, the answer will either have all 0's if that bit wasn't 1, or one 1 if the bit was one. Lucky for us, C interprets any value that's not 0 as "true" in an if-statement.

So, all in all, that define checks whether or not the "i"th bit of "bits" is 1.
Logged

West Des Moines, Iowa USA
Offline Offline
Sr. Member
****
Karma: 2
Posts: 428
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

If I add parentheses it'll be easier to understand:

Code:
#define BIT_IS_SET(i,bits) ((1 << i) & bits)

The macro shifts 1 left i positions and bitwise ands the result with the value bits. The result will be true if bit i of bits is set.

Note: a safer version of this macro would look like:

Code:
#define BIT_IS_SET(i,bits) ((1 << (i)) & (bits))

because it would be "better behaved" if either i or bits happened to be an expression.
Logged

There's always a better way!

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ah! I think I get it now.
In my hopeless confusion, I was thinking this #define was defining a variable called BIT_IS_SET. But, from what you say, this is almost a function right? Which returns true or false depending on the result of bitwise ANDing?

Just one more question, can I do something like 6<<i so that the binary number for 6 (0000011) becomes 00000110 (i.e. 12)?

Thanks a lot for your help though.
Logged

Offline Offline
Full Member
***
Karma: 2
Posts: 175
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi.How work this?
Code:
#define IRpin_PIN      PIND
#define IRpin             2
 
//  while (digitalRead(IRpin)) { // this is too slow!
    while (IRpin_PIN & (1 << IRpin)) {
     // pin is still HIGH
 
     // count off another few microseconds
     highpulse++;
     
Logged

Web / sms / bluetooth / arduino / android remote control. Home Automation projects.
http://www.power7.net

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 642
Posts: 50384
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Just one more question, can I do something like 6<<i so that the binary number for 6 (0000011) becomes 00000110 (i.e. 12)?
If i is 1, yes.
Logged

Pages: [1]   Go Up
Jump to: