Hi,

I just tested the following snippet on TelosB, whose output is also listed.
    int8_t a, b;
    a = -1;
    printf("%d\n", a << 1); //-2
    a = -127;
    b = a << 1;
    printf("%d\n", b); //2
    printf("%d\n", a << 1); //-254
    b = a >> 1;
    printf("%d\n", b); //-64
So the right shift is arithmetic.
Is constant integer regarded as 16 bit signed int? That's why 'b' produces 2
while 'a << 1' -254 because 'a' is promoted to 16 bit signed first.

On Thu, Mar 24, 2011 at 2:14 AM, Eric Decker <[email protected]> wrote:

> tinyos uses nesc which is a superset of C.  Bitshift is what ever C defines
> it as.
>
> And please specify your environment/context at the top of your question.
>  Putting it at the bottom
> makes it real easy to miss.
>
> This really isn't a tinyos question but a basic programming question.   I
> use Harbison and Steele, 4th
> edition, C a reference manual.
>
> My udderstanding is ...
>
> << always shifts in 0 from the right.  Bits shifted off the left are
> discarded.  The size depends on the
> type of the 1st operand.   So it doesn't maintain sign.
>
> >> is a different story.   If the first operand is unsigned or a positive
> signed integral, then 0's are
> shifted in from the left.   If signed and negative it is left at the
> discretion of the implementation.
> Non-portable.  I don't remember off hand what msp430-gcc does.
>
> You can look to see what a local (on the PC) program does.   Or test it out
> in Python.  That will
> tell you what that compiler/cpu will do.   Interesting data point.   Or
> fire up gdb and print
> numbers and expressions.
>
> Or build a simple tinyos program (ie. Blink) and modify it.  Then look at
> the code produced
> via objdump (msp430-objdump).  See the man page and play with it.   You can
> also fire
> up msp430-gdb and do the same thing.  Because it is a msp430 gdb it will
> pretend you are
> futzing with a msp430 processor.
>
> On Wed, Mar 23, 2011 at 8:33 PM, Xiaohui Liu <[email protected]> wrote:
>
>> Hi everyone,
>>
>
>> Is bit shift operation, left and right, in T2 logical or arithmetic? What
>> are the results for the following statements and why?
>> statement 1:
>> (-1) << 2;
>>
>
> Will default to a 16 bit signed int.      0xfffe
>
>>
>>
>> statement 2:
>> (-127) << 1;
>>
>
>  -127 = 0xff81 and << 1 yields 0xff02.
>
>
>>
>> statement 3:
>> int8_t a = -127;  //binary form: 10000001
>> int8_t b = a << 1;
>> Is 'b' equal to 2 (00000010)? If yes, we shift a negative number into a
>> positive one?
>>
>
> yes.
>
>
>>
>>
>> Thanks in advance.
>>
>> --
>> -TelosB
>> -Xiaohui Liu
>>
>> _______________________________________________
>> Tinyos-help mailing list
>> [email protected]
>> https://www.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help
>>
>
>
>
> --
> Eric B. Decker
> Senior (over 50 :-) Researcher
>
>
>


-- 
-Xiaohui Liu
_______________________________________________
Tinyos-help mailing list
[email protected]
https://www.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help

Reply via email to