Go Down

Topic: Syntax Questions (Read 895 times) previous topic - next topic

Flat Stanley

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

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