KBD_COL_PORT &= ~temp; What is the meaning of this notation?

# TO Peg,rhinocerohead and Bigmac,please help !

KBD_COL_PORT &= ~temp; What is the meaning of this notation?

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?

KBD_COL_PORT &= ~temp; What is the meaning of this notation?

Thanks

Ganimides

- 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?- 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 this0000 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 = 15It 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.
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

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