Hi,
~E == (MAX - E) is true as long as you store back the result in a
variable of the same original unsigned type.
The standard says:
"The integer promotions are performed on the operand". An integer
promotion rule says:
"If an int can represent all values of the original type, the value is
converted to an int;"
int's range is wider than uint8_t, therefore ~a is promoted to
(signed) int. Here comes another mechanism: sign extension. The step
by step procedure that gives -6 is:
uint8_t a = 5 ( 00000101b)
~a is b11111010b
~a as 16 bit int: 1111111111111010b = -6d
So the correct fix for Bruno's code is:
if ((255-5) == (uint8_t)~a) {
/* ... */
}
Regards
--
Giuseppe Cardone
On Fri, Apr 23, 2010 at 12:51 AM, Michael Schippling <[email protected]> wrote:
> I'm not sure your interpretation is correct -- this phrase:
>
> "...the expression ~E is equivalent to the maximum value
> representable in that type minus E..."
>
> Seems to say that: ~E == (MAX - E)
> where MAX is 255 for an unsigned byte.
> Which is exactly what the OP was trying to do: 255 - E
>
> If that isn't the case I don't think I could do bit setting:
> mask |= BITFIELD;
> and resetting:
> mask &= ~BITFIELD;
>
> There might be some sign extension foo on signed types,
> but I think the ~bitflip deals with that automagically...
> MS
>
>
> Giuseppe Cardone wrote:
>>
>> Hi,
>>
>> this "problem" is not related to MSP430, nesC or TinyOS. Rather it's
>> related to the C standard. The point 4 from section 6.5.3.3 of the C99
>> standard reads:
>>
>> "The result of the ~ operator is the bitwise complement of its
>> (promoted) operand (that is, each bit in the result is set if and only
>> if the corresponding bit in the converted operand is not set). The
>> integer promotions are performed on the operand, and the result has
>> the promoted type. If the promoted type is an unsigned type, the
>> expression ~E is equivalent to the maximum value representable in that
>> type minus E."
>>
>> (I'm unable to find the C89 standard at this moment, but it says
>> pretty much the same things).
>>
>> Therefore if you do:
>>
>> ~a
>>
>> the type is promoted to signed int, and its value is -6. Since 255-a =
>> 250, the results are different and your comparison returns false.
>>
>> Regards.
>>
>>
>> --
>> Giuseppe Cardone
>>
>>
>>
>> On Thu, Apr 22, 2010 at 5:46 PM, Juchli Bruno HSLU T&A
>> <[email protected]> wrote:
>>>
>>> ---------- Forwarded message ----------
>>> From: Juchli Bruno HSLU T&A <[email protected]>
>>> To: "[email protected]"
>>> <[email protected]>
>>> Date: Thu, 22 Apr 2010 17:46:45 +0200
>>> Subject: Bitwise Not (Complement) Operator @ msp430 / nesc 1.3.1
>>> Dear All,
>>>
>>> i've had some trouble debugging my TinyOS 2.1.1 application today. I've
>>> had some code that essentially was the same as:
>>>
>>> uint8_t a = 5;
>>>
>>> what struck me, was that the following returned FALSE:
>>>
>>> (~a == (255-a))
>>>
>>>
>>> { Note:
>>> b00000101 = 5
>>> b11111010 = ~5 = 250 = (255-5)
>>> }
>>>
>>> Did i miss something or is this a (un-)known problem?
>>>
>>> I ended up using (255-b) instead of ~b.
>>>
>>> regards,
>>> Bruno Juchli
>>
>> _______________________________________________
>> Tinyos-help mailing list
>> [email protected]
>> https://www.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help
>
_______________________________________________
Tinyos-help mailing list
[email protected]
https://www.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help