TO Peg,rhinocerohead and Bigmac,please help !

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

TO Peg,rhinocerohead and Bigmac,please help !

4,749 Views
ganimides
Contributor I
Hi guys!.
 
Could you help to understand what does these code lines do?
 
Let`s suppose KBD_MAX_ROW=4
 
 1) temp = (1 << KBD_MAX_ROW) - 1   Is this code line equal to 7 (0111)?
 
 
  2)Let`s suppose KBD_COL_DDR= DDRA  and KBD_COL_PORT =PORTA
   
    KBD_COL_DDR  |= temp;            What is the meaning of this notation?
    KBD_COL_PORT &= ~temp;       What is the meaning of this notation?

 
Thanks
 
Ganimides
 
Labels (1)
0 Kudos
Reply
5 Replies

628 Views
ganimides
Contributor I

Thank you very much guys!!!!...

I`ve been very ill the last weeks and I just have reincorporated since 2 days ago.

 

Ganimides

0 Kudos
Reply

628 Views
bigmac
Specialist III
Hello ganimides,

ganimides wrote:
Could you help to understand what does these code lines do?
 
Let`s suppose KBD_MAX_ROW=4
 
 1) temp = (1 << KBD_MAX_ROW) - 1   Is this code line equal to 7 (0111)?
 
 2)Let`s suppose KBD_COL_DDR= DDRA  and KBD_COL_PORT =PORTA
   
   KBD_COL_DDR  |= temp;            What is the meaning of this notation?
   KBD_COL_PORT &= ~temp;       What is the meaning of this notation?

1) temp = (1 << KBD_MAX_ROW) - 1
       = (%00010000) - 1
       = %00001111 or 15 decimal

2a) KBD_COL_DDR |= temp;
  is shorthand for
  KBD_COL_DDR = KBD_COL_DDR | temp;
  This sets one or more bits in accordance with the mask value held in temp.

2b) KBD_COL_PORT &= ~temp;
   is shorthand for
   KBD_COL_PORT = KBD_COL_PORT & ~temp;
   This clears one or more bits in accordance with the mask value held in temp.

Regards,
Mac


 

0 Kudos
Reply

628 Views
mjcoury
Contributor I
Pardon my complete ignorance, but how is the first problem 15 and not 7?

KBD_MAX_ROW=4
(1 KBD_MAX_ROW) - 1

the 1 is a bit shift to the left or Multiply by "2"

so 0b00000100 x 2 = 0b1000 or 8... 8 - 1 = 7?
0 Kudos
Reply

628 Views
rhinoceroshead
Contributor I
It is 15.  As you said, the bit shift is exactly like multiplying by 2.  We started with the number 1 and we bit shifted four times, or multiplied by 2 four times.
 
0000 0001 start with this
0000 0010 after 1st bit shift (=2)
0000 0100 after 2nd bit shift (=4)
0000 1000 after 3rd bit shift (=8)
0001 0000 after 4th bit shift (=16)
0000 1111 after subtracting 1 (=15)
 
Alternatively, 1*2*2*2*2 - 1= 2^4 -1 = 15
 
It looks like the code is just trying to generate a bitmask that is justified to the right, given a parameter of how many bits need to be in the mask.
0 Kudos
Reply

628 Views
peg
Senior Contributor IV

Seeing how I was invited and all, I will attempt to make it even a little clearer:

1 << KBD_MAX_ROW

is 1 << 4

<< means left shift the thing on the left by the number of bits on the right

(shift 1 to the left 4 times)

now look at rhino's binary example.

As for the other two, refer to bigmac's expansions then note that:

| means bitwise OR    00001010 | 00000011 = 00001011

& means bitwise AND    00001010 & 00000011 = 00000010

~ means one's complement (NOT)     ~00001010 = 11110101

(values chosen are arbitrary)

Then read bigmac's beautifully concise one sentence explanation for their most common use.

Regards David

 

Message Edited by peg on 2006-07-29 09:29 AM

0 Kudos
Reply