Menu

Some Useful Bit Commands

Anonymous
2014-05-01
2014-05-02
  • Anonymous

    Anonymous - 2014-05-01

    When I was working on the logarithm project, I checked out how people were handling these in other languages. Along the way, I noticed three commands in the Basic Stamp language and wrote my own versions to implement these, for testing purposes. But as it turns out, I think they might be extremely handy to add to Great Cow Basic at the low level (for maximum efficiency), and especially if they were overloaded to handle the various data types. Anyway, here they are:

    function NCD(in arg1 as word) as byte
      ;return the bit position of the highest 1 appearing
    
      NCD = 16                ;count down from here
      do
        set status.c off
        ;in effect, do a shift left until the first 1 found
        rotate arg1 left      ;this is really a shift
        NCD--                 ;keep track of whole part
      loop until status.c     
    end function
    
    function DCD(in arg1 as byte) as word
      ;set the bit indicated by the argument (0 through 15)
    
      DCD = 0
      set status.c on         ;this bit gets moved into place
    
      do
        rotate DCD left       ;again, this is a shift  
        arg1-- 
      loop until arg1 = 255   ;think of this as -1
    end function  
    
    function shiftR (in arg1 as word, in arg2 as word) as word
      ;shift arg1, arg2 places to the right
    
      repeat arg2
        status.c = 0
        rotate arg1 right
      end repeat
      shiftR = arg1
    end function
    

    For the last one, it would be nice to code the parser to recognize it as the operator >>, just to match most other languages.

     
  • Anobium

    Anobium - 2014-05-02

    Nice.

    You be able to create << and >> operators by some clever thinking by using a #define and changing from a function to a sub. This is just a thought - it may not be practical.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.