Announcement

Collapse
No announcement yet.

C to PB - Shifted Values in Constants

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • C to PB - Shifted Values in Constants

    All,

    I have the following flags in C:

    // My flags
    const int MY_FLAG_IGNORE_SOMETHING= 1<<0;
    const int MY_FLAG_IGNORE_ONE= 1<<1;

    Now, how do I preserve the essence of the code, the fact
    a constant is derived from a shifted value? I could always
    replace the shifting with the actual numerical result in a constant.
    Also, I could compute these once at startup. The two latter solutions
    I'd rather avoid. Does anyone have any ideas on the first?

    Thanks,

    Rich

    ------------------
    _____________________________________________________________________________________
    It's hard when you're up to your armpits in alligators to remember you came here to drain the swamp.
    President Reagan
    February 10, 1982
    _____________________________________________________________________________________
    It's hard when you're up to your armpits in alligators to remember you came here to drain the swamp.
    President Reagan
    February 10, 1982

  • #2
    Rich --

    1<<0 means "start with the value 1 and shift it 0 bits to the left"

    1<<1 means "start with the value 1 and shift it 1 bit to the left"

    And so on.

    PowerBASIC does not support shift or exponentiation operators in equate-definition lines, so I would recommend:
    Code:
    %MY_FLAG_IGNORE_SOMETHING = 1
    %MY_FLAG_IGNORE_ONE       = 2
    'the next value would be 4, then 8, 16, 32...
    Or you could stick a little closer to the spirit of the original by using this:
    Code:
    %MY_FLAG_IGNORE_SOMETHING = &b00000001
    %MY_FLAG_IGNORE_ONE       = &b00000010
    'the next value would be... &b00000100
    -- Eric


    ------------------
    Perfect Sync Development Tools
    Perfect Sync Web Site
    Contact Us: mailto:[email protected][email protected]</A>



    [This message has been edited by Eric Pearson (edited February 21, 2003).]
    "Not my circus, not my monkeys."

    Comment


    • #3
      Originally posted by Eric Pearson:
      Rich --

      1<<0 means "start with the value 1 and shift it 0 bits to the left"

      1<<1 means "start with the value 1 and shift it 1 bit to the left"

      And so on.

      PowerBASIC does not support shift or exponentiation operators in equate-definition lines, so I would recommend:
      Code:
      %MY_FLAG_IGNORE_SOMETHING = 1
      %MY_FLAG_IGNORE_ONE       = 2
      'the next value would be 4, then 8, 16, 32...
      Or you could stick a little closer to the spirit of the original by using this:
      Code:
      %MY_FLAG_IGNORE_SOMETHING = &b00000001
      %MY_FLAG_IGNORE_ONE       = &b00000010
      'the next value would be... &b00000100
      -- Eric

      Eric,

      I appreciated your reply. Unfortunately, I am aware PB does not support bit shifting in equates. To keep in the spirit of the original code, I think I'm going to use your following suggestion:

      %MY_FLAG_IGNORE_SOMETHING = &b00000001
      %MY_FLAG_IGNORE_ONE = &b00000010
      'the next value would be... &b00000100


      Thanks,

      Rich

      ------------------
      _____________________________________________________________________________________
      It's hard when you're up to your armpits in alligators to remember you came here to drain the swamp.
      President Reagan
      February 10, 1982
      _____________________________________________________________________________________
      It's hard when you're up to your armpits in alligators to remember you came here to drain the swamp.
      President Reagan
      February 10, 1982

      Comment

      Working...
      X