Comment on Lesson 5

For makers who have purchased an Adafruit Starter Pack, get help with the tutorials here!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
nathan42100
 
Posts: 2
Joined: Fri Jul 25, 2008 10:08 pm

Comment on Lesson 5

Post by nathan42100 »

A more efficient way in my opinion for the lightMode variable would be to use a modulus:

Code: Select all

lightMode = lightMode%4 + 1;

mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am

Re: Comment on Lesson 5

Post by mtbf0 »

nathan42100 wrote:A more efficient way in my opinion for the lightMode variable would be to use a modulus:

Code: Select all

lightMode = lightMode%4 + 1;
actually modulus is not terribly efficient on a li'l chip with no hardware divide. as long as your division is by a power of 2, though, you can go with...

Code: Select all

lightmode = (lightmode + 1) & 3;
who knows maybe gcc will optimize your modulus to a bitwise and anyway.

nathan42100
 
Posts: 2
Joined: Fri Jul 25, 2008 10:08 pm

Post by nathan42100 »

Well, I never knew about &. What exactly does it do?

mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am

Post by mtbf0 »

it is a bitwise, (as opposed to logical, eg &&), "and" operation.

feel free to google bitwise and. finding a satisfactory definition will depend on how much you know about boolean logic.

for our purposes what you need to know is that for n, a power of two,

Code: Select all

x & (n - 1)
will give the same result as

Code: Select all

x % n

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Starter Pack”