Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    how to change some bits in an byte result

    Scheduled Pinned Locked Moved MicroPython
    micropythoni2c
    6 Posts 3 Posters 1.5k Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J Offline
      Jiemde Pybytes Beta
      last edited by

      Hi All, How to change some bits, with a mask in an byte result from the i2c. readfrom_mem() ?
      Trying a lot of solution but all finish by an error!
      Is someone can explain how to do it ?
      Thanks for help

      Jiemde

      L 1 Reply Last reply Reply Quote 0
      • L Offline
        livius @Jiemde
        last edited by livius

        @jiemde
        below common operators

        &
        |
        <<
        >>
        

        for changing bit to 1 do
        value | (1<<bit_number)
        to 0 do:
        value & ~(1<<bit_number)
        e.g.

        v=4
        v5=v | 1
        v6=v | (1<<1)
        print(v5)
        print(v6)
        

        result should be v5=5 v6=6

        think about people first, then computers

        J 1 Reply Last reply Reply Quote 1
        • J Offline
          Jiemde Pybytes Beta @livius
          last edited by

          Hi @livius, Ok, thanks but I know that. The problem is when the result of the i2c.readfrom_mem() is for example b'\x34'
          example:
          value = i2c.readfrom_mem(device addr, register addr, 1) the result is b'\x34'
          value &= ~(0b00000111 << 4)
          value &| (mode << 4)
          I always obtain an error !

          robert-hhR 1 Reply Last reply Reply Quote 0
          • robert-hhR Offline
            robert-hh @Jiemde
            last edited by robert-hh

            @jiemde said in how to change some bits in an byte result:

            You have to take value[0], to get an integer. Like

            value[0] &= ~(0b00000111 << 4)
            value[0] |=  (mode << 4)
            

            and to get it back into a bytes value:

            result = bytes([value[0] & (~(0b00000111 << 4))])
            

            (These many brackets look like LISP, arrgh!!!)

            J 1 Reply Last reply Reply Quote 3
            • J Offline
              Jiemde Pybytes Beta @robert-hh
              last edited by

              Hi @robert-hh, Already try your first 2 lines with value[0] without success ! already an error " TypeError: 'bytes' object does not support item assignment"

              Jiemde

              L 1 Reply Last reply Reply Quote 0
              • L Offline
                livius @Jiemde
                last edited by livius

                @jiemde
                bytes is immutable you must first convert it to mutable type like bytearray
                e.g.

                a=bytes([1,2,3]) #create bytes in your case steep not needed you have it already as result from readfrom_mem
                b=bytearray(a) #convert it to bytearray
                b[1]=11 #change value to what you need
                a=bytes(b) #change it back to bytes if you need to
                

                think about people first, then computers

                1 Reply Last reply Reply Quote 1

                Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                With your input, this post could be even better 💗

                Register Login
                • 1 / 1
                • First post
                  Last post

                Pycom on Twitter

                Pycom