SetBit

Home Forums General Programming SetBit

Tagged: 

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #67998
    Possemo
    Participant
      • Topics: 14
      • Replies: 638
      • Total: 652
      • ★★★

      Hi I just don’t get how to set bits in a byte. Extracting bits is very easy and is working perfectly.

      	by=CtrlrLuaBigInteger(0)
      	by:setBit(1,7)

      This throws the following error:

      At line [-1]: [C]
      What: C
      Namewhat: method
      Name: setBit
      Error message: No matching overload found, candidates:
      void setBit(CtrlrLuaBigInteger&,int,bool)

      Could someone point me in the right direction?
      Thanks

      #67999
      atom
      Keymaster
        • Topics: 159
        • Replies: 2945
        • Total: 3104
        • ★★★★★

        You specify the position and a true/false value if the bit should be set or not

        so to set bit 7

        setBit(7,true)
        to clear bit 7
        setBit(7,false)

        etc.

        #68001
        Possemo
        Participant
          • Topics: 14
          • Replies: 638
          • Total: 652
          • ★★★

          Thanks atom, now I see that the error message was quite clear…

          #68032
          Possemo
          Participant
            • Topics: 14
            • Replies: 638
            • Total: 652
            • ★★★

            I am stuck again. Maybe someone could help?

            	-- Byte 48 ***********************************************************
            	byte48=CtrlrLuaBigInteger(0)
            	APorta=panel:getModulatorByName("UpperPortamentoSw"):getModulatorValue()
            	BHold =panel:getModulatorByName("LowerHoldSwitch")  :getModulatorValue()
            
            	    if APorta==0 then byte48:setBit(4,false)
            	elseif APorta==1 then byte48:setBit(4,true)
            	end
            	    if BHold ==0 then byte48:setBit(2,false)
            	elseif BHold ==1 then byte48:setBit(2,true)
            	end
            	PatchTable[49]=string.format("%.2x",Byte48)

            The last line does not work. I cannot write the Byte into the Table:

            Error message: [string “putToLibrary”]:179: bad argument #2 to ‘format’ (number expected, got userdata)

            How can I convert the userdata into something useable, like a number or a string?

            Thanks for reading 🙂

            #68033
            atom
            Keymaster
              • Topics: 159
              • Replies: 2945
              • Total: 3104
              • ★★★★★

              Have a look at the BigInteger documentation http://www.juce.com/doc/classBigInteger

              or just do what(Byte48) to see all methods registered for any object in Lua, there is a toInteger() method that you can use in this case.

              #68034
              Possemo
              Participant
                • Topics: 14
                • Replies: 638
                • Total: 652
                • ★★★

                thanks atom! It pointed me in the right direction.
                This worked for me:

                byte32=CtrlrLuaBigInteger(0)
                
                byte32:setBit(2,true)
                byte32:setBit(0,true)
                
                teststring=byte32:toString(16,2)

                The string shows the number as Hex(16) with two digits(2)

                #68035
                Possemo
                Participant
                  • Topics: 14
                  • Replies: 638
                  • Total: 652
                  • ★★★

                  No, this was not right. We get still userdata with toString() method. As you said for this we need the toInteger method:

                  byte56=CtrlrLuaBigInteger(0)
                  
                  byte56:setBit(6,true)
                  byte56:setBit(3,true)
                  
                  integerValue=byte56:getBitRangeAsInt(0,8)

                  start from first Bit(0), range is eight Bits(8)

                  Edit: Oops, it seems that for this you have to define the range as eight Bits(8). I don’t need the 8th Bit but it gave wrong results with a range of 7 Bits…

                  • This reply was modified 8 years, 3 months ago by Possemo.
                  • This reply was modified 8 years, 3 months ago by Possemo.
                Viewing 7 posts - 1 through 7 (of 7 total)
                • The forum ‘Programming’ is closed to new topics and replies.
                There is currently 0 users and 108 guests online
                No users are currently active
                Forum Statistics
                Threads: 2,495, Posts: 17,374, Members: 77,605
                Most users ever online was 12 on January 22, 2019 3:47 pm
                Ctrlr