Shift Left operator

I have a line in my code that I am trying to understand. It is:

LPC_PINCON->PINSEL3 &= ~(3UL<<30);

I understand the result but still do not completely understand the mechanics of

~(3UL<<30)

Could someone walk me through what is happeneing bit by bit in this?

Thanks

I assume it is answered in here somewhere but after a long search, I sure can't find it.
Last edited on
3UL is the value of 3 considered an unsigned long int.
<< is the bitshift left operator.
3UL<<30 shifts 3UL to the left by 30 bits.
~ is the bitwise "not" operator, which flips all bits.
Last edited on
Hi Catfish
So if I understand this correctly it goes from

0000 0000 0000 0000 0000 0000 0000 0011 = 3ul

to

1100 0000 0000 0000 0000 0000 0000 0000 = 3221225472ul

or 0xC0000000


Seems like a lot of work for nothing. Is there a reason that people use the << instead of just saying the hex for the value they want?

... but don't forget the final result is:
0011 1111 1111 1111 1111 1111 1111 1111
which is then bitwise AND'd with something.

Seems like a lot of work for nothing. Is there a reason that people use the << instead of just saying the hex for the value they want?

It seems this particular programmer couldn't be bothered simply writing a constant.
Operators are most often used on values only known at runtime, the code you posted is a bad counter-example.
Topic archived. No new replies allowed.