Shop OBEX P1 Docs P2 Docs Learn Events
Large integers and bitwise operation — Parallax Forums

Large integers and bitwise operation

diafysaldiafysal Posts: 92
edited 2005-07-26 00:01 in General Discussion
Could someone please translate this:

NegMask CON %1111100000000000 ' For 11-bit negative to 16-bits

IF (y.BIT10 = 1) THEN y = y | NegMask ' Store 11-bits as signed word


(from "TestHm55bCompass.bs2" )

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2005-07-25 05:22
    The operation makes all high bits b15-b11 high when b10 is high.

    That is because negative numbers are stored in 2-complement.

    -1 is %1111111111111111

    If you define b10 to be the sign bit, all bits higher than b10 must be

    set to 1 if b10 is 1.

    regards peter
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2005-07-25 06:34
    in java:

    if ((y & %0000010000000000) != 0) y |= %1111100000000000;



    regards peter




    Post Edited (Peter Verkaik) : 7/25/2005 7:53:37 AM GMT
  • diafysaldiafysal Posts: 92
    edited 2005-07-25 09:51
    There were a slight problem though. "Syntax: Unexpected symbol ignored"

    I have got the feeling that i only can write numbers in dec. hex. or octal.

    what about this:
    if (y > 1023) {
    y = y << 5;
    y = y >> 5;
    }

    ?
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2005-07-25 10:16
    Use hexadecimal numbers instead

    ·· if ((y & 0x0400) != 0) y |= 0xF800;
    regards peter
    ·
  • diafysaldiafysal Posts: 92
    edited 2005-07-25 22:09
    Then I get :

    Error.... .. main contains a constant out of range...

    because of the " 0xf800 " I think.
  • diafysaldiafysal Posts: 92
    edited 2005-07-25 22:20
    This seams to work:

    if ((y & 0x0400) != 0) {
    y = y << 5;
    y = y >> 5;
    }

    For converting 11bit signed integer to 16bit signed integer.
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2005-07-25 22:21
    Ok, use

    ·· if ((y & 0x0400) != 0) y |= (short)0xF800;

    regards peter
    ·
  • diafysaldiafysal Posts: 92
    edited 2005-07-25 22:40
    Hmmm seams to work (wonder why).
    Is " (short)0xf800 " a way to trick the javelin stamp IDE?

    *continues testing*
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2005-07-25 23:48
    A short is a 16bit integer, as is int.

    Where int has a range -32768 to +32767,

    a short has a range 0 to 65535.

    Both types can represent 0x0000 to 0xFFFF but they

    are interpreted differently.

    regards peter
  • diafysaldiafysal Posts: 92
    edited 2005-07-26 00:01
    Thank you for the help.

    regards Lars
Sign In or Register to comment.