Go Down

### Topic: Syntax Questions (Read 1 time)previous topic - next topic

#### Flat Stanley

##### Dec 07, 2005, 05:41 am
basic syntax question....

in the LED driver tutorial there is this line...

dato>>=1;

what does this do to the dato variable??
my hobby --  www.jonathanbryan.com

my life --  www.ifranks.com  [link]www.silver-collector.com[/link] [link]www.da

#### David Cuartielles

#1
##### Dec 07, 2005, 02:46 pm
Hej,

this is the same as:

dato = dato >> 1

what means to shift the variable dato one bit to the right. For example, if dato was A, ASCII's representation of it would be the number 65. This number's binary representation is 01000001.

If you shift that number one bit to the right, you are displacing all the bits in it one position to the right, and putting zeroes on the left side to fill up the spaces.

dato = dato >> 1

would then be:

dato = 01000001 >> 1 = 00100000(1) = 00100000

In the previous formula, the one between brakets or (1) represents the digit that is lost when shifting all the digits to the right. The bolded zero represent the digit we add to the number when shifting.

- - - - - -

That was for the question. And now, the explanation about why we do this type of operations. As you know, numbers in digital electronics are represented in binary logics. Thus we use only two symbols to represent all the possible numbers. Those two symbols are one and zero. A letter, or number can then be represented as an array of those symbols. This opens up a whole bunch of new operations to be done to the numbers etc.

Just to mention an example, to shift a number to the right one position is the same as to divide the number by two leaving out the decimal part of the result. Therefore, in our case:

65 / 2 = 65 >> 1 = 32

In the same way, to shift to the left means to multiply by two, etc.

/David

Go Up

Please enter a valid email to subscribe

### Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy