~ confusing me?

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Amazing, I have worked with bit logic for 8 years ( well at least constantly in this 8 years ) and this just now stumps me. I use ~ all the time to check inverted pins state and it always works as it should. I sure hope there is an easy explanation for this one.

I want to do a compare on this.
( ~0b11111110 == 0b00000001)
I woudl have though this was true, but I'm wrong? Yet both values are 254.

So then I figure order of op, but no... Then I figure it was because I have to value it first ( yeah grabbing at straws now )

char a = ~0b00000001;
if ( a == 0b11111110)

Still not true, what in the heck am I missing here?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Do everything unsigned.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

wow thx, I guess char is the default register type for if comparison? Casting them to uchar did it.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I don't think the problem is signed/unsigned, I think the problem is that "anonymous" values in C are integers, so 16 bits in the case of AVR. So every constant you wrote has 8 more 0 bits in the high byte (which operators such as ~ will operate on).

But then I'm not a language laywer...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

I don't think the problem is signed/unsigned, I think the problem is that "anonymous" values in C are integers, so 16 bits in the case of AVR. So every constant you wrote has 8 more 0 bits in the high byte (which operators such as ~ will operate on).

Well that was actually my point too - the point was that int's are subject to sign extension while "unsigned int" or "unsigned char" are not.