[resolved] mask to 7 bits

Home Forums General Programming [resolved] mask to 7 bits

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #12466
    msepsis
    Participant
    • Topics: 219
    • Replies: 732
    • Total: 951
    • ★★★

    I’m working with a checksum value whos requirements are:

    Sum of all data bytes truncated to 7 bits. The addition is done
    in 8 bit format, the result is masked to 7 bits (00h to 7Fh).

    How do i mask a value to 7 bits in Lua? I did some if statements to see how large the sum value is then subtracted 128 or 256 from it to get the value lower than 127 but I’m wondering if there’s a more elegant way with Lua?

    • This topic was modified 8 years, 8 months ago by msepsis.

    Monstrum Media | Music, Sound & Software Design, Chicago / San Francisco listen

    #12476
    zeoka
    Participant
    • Topics: 73
    • Replies: 466
    • Total: 539
    • ★★★

    (MyData%127) ?
    see the topic Math round() by me
    you can do a modulo

    #12527
    msepsis
    Participant
    • Topics: 219
    • Replies: 732
    • Total: 951
    • ★★★

    Thanks Zeoka, just to clarify the simple answer in clear language:

    The character % will round to the proceeding value.

    myValue % 128

    will round “myValue” to 7 bits.

    It’s that simple, no other reference or links needed 🙂

    • This reply was modified 8 years, 8 months ago by msepsis.

    Monstrum Media | Music, Sound & Software Design, Chicago / San Francisco listen

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

    Module % gives you the remainder http://en.wikipedia.org/wiki/Modulo_operation

    If you want only the last 7 bits of a 16bit value you can use BigInteger http://www.juce.com/juce/api/classBigInteger.html

    myValue = 12300
    bi = BigInteger(myValue)
    last7Bits = bi:getBitRangeAsInt(0,7) -- get 7 bits from bit 0
    

    BUT if you want to scale a large range of numbers, like 0 – 13000 you need a more complex operation (downsample?), something like

    myValue = 456
    myLargeMax = 13000
    v = (myValue / myLargeMax) * 127
    console (string.format ("v=%d",v))
    -- v = 4, 13000 will be 127 etc.
    

    here “v” is the scaled down value

    OR

    myMax = 14000              -- this is our MAX
    x     = 456                -- we want to scale this
    v     = (x / myMax) * 127  -- this is x scaled down
    bi = BigInteger(v)         -- put v into BITS
    console (bi:toString(2,8)) -- print the bits on the console, v i actualy 4.1365714285714
    
    -- 00000100 this will give you 8 bits of the calculated value, there are more, sine all numbers in Lua are of type "double" so there is more bits. You can safely put the result of bi:getBitRangeAsInt(0,7) as a byte in a MIDI message
    

    Or even better

    myMax = 14000
    x     = 456
    v     = (x / myMax) * 127
    r,q    = math.modf(v)
    bi    = BigInteger(v)
    console ("v="..v)
    console ("r="..r)
    console ("q="..q)
    
    v=4.1365714285714
    r=4
    q=0.13657142857143
    
    • This reply was modified 8 years, 8 months ago by atom.
    • This reply was modified 8 years, 8 months ago by atom.
    #12606
    zeoka
    Participant
    • Topics: 73
    • Replies: 466
    • Total: 539
    • ★★★

    Very complete demo. useful
    Thank you

    #13799
    zeoka
    Participant
    • Topics: 73
    • Replies: 466
    • Total: 539
    • ★★★

    I’m not sure about %127 modulo This is not working with the RA

    I tried %128 and this working

    %127 is working until checksum 7e and after this makes chksum errors

    
    --
    -- Called when a modulator value changes
    -- @mod   http://ctrlr.org/api/class_ctrlr_modulator.html
    -- @value    new numeric value of the modulator
    --
    tempo2 = function(mod, value)
    
    progtmp = panel:getModulatorByName("Program Tempo"):getModulatorValue()
     prgtxt = panel:getComponent("text PRG Tempo")
     prgval = progtmp / 10 
    prchks1 = (83+(progtmp / 128))%128
    prchks2 = (84+(progtmp%128))%128
    panel:sendMidiMessageNow(CtrlrMidiMessage{0xF0,0x3E,0x11,0x00,0x21,0x20,0x00,0x00,0x12,(progtmp / 128),prchks1,0xF7})
    panel:sendMidiMessageNow(CtrlrMidiMessage{0xF0,0x3E,0x11,0x00,0x21,0x20,0x00,0x00,0x13,(progtmp%128),prchks2,0xF7})
    
           if progtmp == 0 then 
              prgtxt:setComponentText (string.format ( "Global"))
       elseif progtmp == 1 then 
              prgtxt:setComponentText (string.format ( "Extern"))
       elseif progtmp >= 2 and progtmp <= 99 then 
              prgtxt:setComponentText (string.format ( "00%.1f  Bpm",prgval))
       elseif progtmp >= 100 and progtmp <= 999 then 
              prgtxt:setComponentText (string.format ( "0%.1f  Bpm",prgval))
       elseif progtmp >= 1000 then
              prgtxt:setComponentText (string.format ( "%.1f  Bpm",prgval))
          end
    end

    Can you give a feedback please Msepsis if you encounter errors with your chksum stuff ?

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

    Modulo operation just checks the result of the division of one number by the other (explained in detail) http://en.wikipedia.org/wiki/Modulo_operation) i doubt it will work as a checksuming mechanism for anything. Find out exactly what you need.

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

    Here is a few modulot operation results to better illustrate what this does

    0 % 128 = 0
    16 % 128 = 16
    32 % 128 = 32
    48 % 128 = 48
    64 % 128 = 64
    80 % 128 = 80
    96 % 128 = 96
    112 % 128 = 112
    128 % 128 = 0
    144 % 128 = 16
    160 % 128 = 32
    176 % 128 = 48
    192 % 128 = 64
    208 % 128 = 80
    224 % 128 = 96
    240 % 128 = 112
    256 % 128 = 0
    272 % 128 = 16
    288 % 128 = 32
    304 % 128 = 48
    320 % 128 = 64
    336 % 128 = 80
    352 % 128 = 96
    368 % 128 = 112
    384 % 128 = 0
    400 % 128 = 16
    416 % 128 = 32
    432 % 128 = 48
    448 % 128 = 64
    464 % 128 = 80
    480 % 128 = 96
    496 % 128 = 112
    
Viewing 8 posts - 1 through 8 (of 8 total)
  • The forum ‘Programming’ is closed to new topics and replies.
There is currently 0 users and 63 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