Getting Started

(lily) #1

Chapter 4: C Types, Operators, and Expressions


nd 2:

m

which does the following:


myByte = 01101011 = 0x6B
0x40 = 11111010 = 0xFA




AND = 01101010 = 0x6A


where the ‘&’ and

ter than or equal to ‘a’


  1. So if the
    ppercase version
    subtracting as in
    ll, it is more efficient for the machine to take the inverse of the


00
~0x20 = 11011111

myByte = 00101011 = 0x2B
0x40 = 01000000 = 0x40


AND = 01101011 = 0x6B


Next we want to clear bits 0 a


yByte &= 0xFA;


-- --

So in summary we set bits with ‘|’ and clear bits with ‘&’.


The Butterfly Software has a clever snippet in LCD_driver.c
‘~’ operators are used to convert a lowercase letter to a capital:


// c is a letter
if (c >= 'a') // Convert to upper case
c &= ~0x20; // if necessary


This statement first checks to see if the character c is grea
and uses the convenient fact that in ASCII the letters are sequential with the
capitals beginning at 0x41 and the lowercase beginning at 0x6
character is >= 0x61 then it is lowercase and we can derive the u
by subtracting 0x20. So why do we use ‘c &= ~0x20’ instead of
‘c -= 0x20’? We
minuend and then AND it with the subtrahend (this by the way, is the first time
since grammar school that I’ve actually used minuend and subtrahend, I’m
amazed that these terms actually stuck. Maybe it was the teachers steely glare and
the dangerous looking pointer she held.) Let’s look at it shall we?


0x20 = 001000

Free download pdf