Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. How to use bit operations
Forum Updated to NodeBB v4.3 + New Features

How to use bit operations

Scheduled Pinned Locked Moved Unsolved C++ Gurus
19 Posts 6 Posters 7.8k Views 1 Watching
  • 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.
  • fcarneyF Offline
    fcarneyF Offline
    fcarney
    wrote on last edited by
    #3

    @K-Str Where are you getting your bits? Are you bit banging a microcontoller?

    C++ is a perfectly valid school of magic.

    1 Reply Last reply
    0
    • K-StrK Offline
      K-StrK Offline
      K-Str
      wrote on last edited by
      #4

      @jsulm
      Thank you very much for your answer.
      Yes it works in this way.
      @fcarney
      Thank you very much for your answer.
      I get the data from a RX868 receiver. It delivers the data bitwise.
      II think it is a bitstream.
      Kurt

      1 Reply Last reply
      0
      • K-StrK Offline
        K-StrK Offline
        K-Str
        wrote on last edited by K-Str
        #5

        Hi,
        thanks for your answers,
        But it doesn't solve my problem:
        I get every 200μs data from a device. It seems this are single bit in an integer value .
        To analyze the incomming data I would like to collect 200 bits and store it in one value of 200 bit length.
        Is there any way to do this like :

        value bit x(NbrOfBits=200)
        int input;
        input = ReadFromDevice();
        ShiftLleft value;
        value = value + input; **OR** value = value XOR input;
        

        Is there a simply solution?
        Kurt

        jsulmJ 1 Reply Last reply
        0
        • K-StrK K-Str

          Hi,
          thanks for your answers,
          But it doesn't solve my problem:
          I get every 200μs data from a device. It seems this are single bit in an integer value .
          To analyze the incomming data I would like to collect 200 bits and store it in one value of 200 bit length.
          Is there any way to do this like :

          value bit x(NbrOfBits=200)
          int input;
          input = ReadFromDevice();
          ShiftLleft value;
          value = value + input; **OR** value = value XOR input;
          

          Is there a simply solution?
          Kurt

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #6

          @k-str What are you going to do with all these bits when you got all of them?
          What kind of data do they represent?
          https://en.cppreference.com/w/cpp/language/bit_field comes to mind.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • K-StrK Offline
            K-StrK Offline
            K-Str
            wrote on last edited by
            #7

            @jsulm I want to read data comming bitwise from a weather station.
            This have to be analysed by searching the begin of the data block maybe its 00000001.
            Then I can find the other data like temperature or wind speed. But first I need all of the bits stored in a variable.

            aha_1980A 1 Reply Last reply
            0
            • K-StrK K-Str

              @jsulm I want to read data comming bitwise from a weather station.
              This have to be analysed by searching the begin of the data block maybe its 00000001.
              Then I can find the other data like temperature or wind speed. But first I need all of the bits stored in a variable.

              aha_1980A Offline
              aha_1980A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on last edited by aha_1980
              #8

              @k-str said in How to use bit operations:

              @jsulm I want to read data comming bitwise from a weather station.

              Ok, may you be a bit more specific? How is that data coming from the weather station? Serial port? Bluetooth? Network? Do they provide an API to read out the data?

              This have to be analysed by searching the begin of the data block maybe its 00000001.

              And as you can see, these are eight bit which form a byte. There is no modern computer that operates on smaller units, and no transport medium that allows you to transport single bits (well at least I've never seen one, I'm glad to hear opposite examples. And please: bit-banging on a MCU does not count).

              In short: wherever you get the data from, it will be an array of bytes. Maybe call it bitstream, but's an array of bytes. Dot.

              Then I can find the other data like temperature or wind speed. But first I need all of the bits stored in a variable.

              Is there a documentation for that data? Or do you reverse-engineer the protocol?

              It is easier for us to help you if you provide what you know for sure, not what you assume.

              Regards

              Qt has to stay free or it will die.

              1 Reply Last reply
              2
              • K-StrK Offline
                K-StrK Offline
                K-Str
                wrote on last edited by K-Str
                #9

                @aha_1980 ,
                thanks for your answer!
                Tthe weather station sends data via 868 Mhz radio signal.
                Here the detailed configuration:

                • Weather station : WH1080
                • Computer : Raspberry zero
                • Radtio receiver : RX868SH-DV

                Here some information of the interface :
                The incomming data are :
                version 1:

                The transmitter sends 11 bytes of data as follows. Some of these differ from any of the documentation I’ve found on Fine Offset weather stations, so I’ve described them here (letters to the nibbles for the description below): –

                Byte     0  1  2  3  4  5  6  7  8  9
                Nibble  ab cd ef gh ij kl mn op qr st
                Example a1 82 0e 5d 02 04 00 4e 06 86
                
                I’ve interpreted the nibbles as follows: –
                
                    abc: device identifier
                    def: temperature
                    gh: humidity
                    ij: average wind speed low byte
                    kl: gust wind speed low byte
                    m: unknown
                    n: rainfall counter high nibble
                    op: rainfall counter
                    q: battery-low indicator
                    r: wind direction
                    st: checksum
                

                version 2:

                00000001 	T1T2T3T41 	A1A2A3V1 	T11T12T13T141 	T21T22T23T241 	T31T32T33T341 	F11F12F13F141 	F21F22F23F241 	W11W12W13W141 	W21W22W23W241 	W31W32W33W341 	C11C12C13C141 	C21C22C23C241 	C31C32C33C341 	B1B2B3B41 	Q1Q2Q3Q41 	S1S2S3S41
                Präambel 	___7___ 	1_R_0_V 	____0.1°___ 	____1°_____ 	____10°____ 	____1%_____ 	____10%____ 	_0.1 km/h__ 	___1 km/h__ 	__10 km/h__ 	___R_LSN___ 	___R_MID___ 	___R_MSN___ 	__???___ 	_Check_ 	_Summe_
                
                
                R: acrual rain (1 = Regen)
                V: sign of Temperatur (1 = negativ)
                W1..W3: 3 * 4Bit Wind speed (BCD)
                C1..C3: 12 Bit rain counter
                T1..T3: 3 * 4Bit Temperatur (BCD)
                F1..F2: 2 * 4Bit humidity (BCD)
                

                I think the first version is the better one,
                I will check for the pattern 0xa18 which is the device identifier.
                If it doesn't work I try version 2.
                Checking for unsigned char = 1.

                Do you think this is ok?
                If you need further information please ask.

                aha_1980A 1 Reply Last reply
                0
                • K-StrK K-Str

                  @aha_1980 ,
                  thanks for your answer!
                  Tthe weather station sends data via 868 Mhz radio signal.
                  Here the detailed configuration:

                  • Weather station : WH1080
                  • Computer : Raspberry zero
                  • Radtio receiver : RX868SH-DV

                  Here some information of the interface :
                  The incomming data are :
                  version 1:

                  The transmitter sends 11 bytes of data as follows. Some of these differ from any of the documentation I’ve found on Fine Offset weather stations, so I’ve described them here (letters to the nibbles for the description below): –

                  Byte     0  1  2  3  4  5  6  7  8  9
                  Nibble  ab cd ef gh ij kl mn op qr st
                  Example a1 82 0e 5d 02 04 00 4e 06 86
                  
                  I’ve interpreted the nibbles as follows: –
                  
                      abc: device identifier
                      def: temperature
                      gh: humidity
                      ij: average wind speed low byte
                      kl: gust wind speed low byte
                      m: unknown
                      n: rainfall counter high nibble
                      op: rainfall counter
                      q: battery-low indicator
                      r: wind direction
                      st: checksum
                  

                  version 2:

                  00000001 	T1T2T3T41 	A1A2A3V1 	T11T12T13T141 	T21T22T23T241 	T31T32T33T341 	F11F12F13F141 	F21F22F23F241 	W11W12W13W141 	W21W22W23W241 	W31W32W33W341 	C11C12C13C141 	C21C22C23C241 	C31C32C33C341 	B1B2B3B41 	Q1Q2Q3Q41 	S1S2S3S41
                  Präambel 	___7___ 	1_R_0_V 	____0.1°___ 	____1°_____ 	____10°____ 	____1%_____ 	____10%____ 	_0.1 km/h__ 	___1 km/h__ 	__10 km/h__ 	___R_LSN___ 	___R_MID___ 	___R_MSN___ 	__???___ 	_Check_ 	_Summe_
                  
                  
                  R: acrual rain (1 = Regen)
                  V: sign of Temperatur (1 = negativ)
                  W1..W3: 3 * 4Bit Wind speed (BCD)
                  C1..C3: 12 Bit rain counter
                  T1..T3: 3 * 4Bit Temperatur (BCD)
                  F1..F2: 2 * 4Bit humidity (BCD)
                  

                  I think the first version is the better one,
                  I will check for the pattern 0xa18 which is the device identifier.
                  If it doesn't work I try version 2.
                  Checking for unsigned char = 1.

                  Do you think this is ok?
                  If you need further information please ask.

                  aha_1980A Offline
                  aha_1980A Offline
                  aha_1980
                  Lifetime Qt Champion
                  wrote on last edited by aha_1980
                  #10

                  Hi @k-str,

                  now it get's interesting - low level programming :)

                  Computer : Raspberry zero
                  Radtio receiver : RX868SH-DV

                  Still unclear for me how your receive the data in the PI. Is the receiver connected to and toggles a GPIO pin? How do you sample the data?

                  The transmitter sends 11 bytes of data as follows.

                  That's good. So you will need 11 bytes to store the complete data. Plus a bit of handling to detect the start of the frame etc.

                  Then the fun begins:

                  #include <array>
                  #include <iostream>
                  
                  using namespace std;
                  
                  int main()
                  {
                      std::array<uint8_t, 11> data;
                      
                      // add sample data
                      data[0] = 0x12;
                      data[1] = 0x30;
                      data[2] = 0x26;
                  
                      // calculate vendor and temperature as example
                      const uint16_t vendor = (data[0] << 4) | (data[1] >> 4);
                      const uint16_t temperature = ((data[1] & 0x0F) << 8) | data[2];
                  
                      cout << hex << "vendor: " << vendor << endl;
                      cout << dec << "temperature: " << temperature << endl;
                  
                      return 0;
                  }
                  
                  // Output:
                  vendor: 123
                  temperature: 38
                  

                  The other data can be used in similar ways. If both nibbles are in the same byte, you can just use it. If they are distributed over several bytes, you need to extract the nibbles and shift. BCD just means, that each nibble encodes a decimal digit of 0..9.

                  I hope that helps you.

                  Regards

                  [Edit: fixed typo in temperature shift]

                  Qt has to stay free or it will die.

                  1 Reply Last reply
                  3
                  • K-StrK Offline
                    K-StrK Offline
                    K-Str
                    wrote on last edited by
                    #11

                    @aha_1980
                    Thanks for your answer:
                    yes it is connected to GPIO,
                    The DATA is connectet to pin 13 (GPIO02) as input
                    The EN = RX Enable is connected to pin 15 (GPIO03) as output set to on
                    Kurt

                    aha_1980A 1 Reply Last reply
                    0
                    • K-StrK K-Str

                      @aha_1980
                      Thanks for your answer:
                      yes it is connected to GPIO,
                      The DATA is connectet to pin 13 (GPIO02) as input
                      The EN = RX Enable is connected to pin 15 (GPIO03) as output set to on
                      Kurt

                      aha_1980A Offline
                      aha_1980A Offline
                      aha_1980
                      Lifetime Qt Champion
                      wrote on last edited by
                      #12

                      @k-str said in How to use bit operations:

                      The DATA is connectet to pin 13 (GPIO02) as input

                      And where do you get clock from? Or how do you know when a bit starts and ends?

                      Qt has to stay free or it will die.

                      JonBJ 1 Reply Last reply
                      0
                      • K-StrK Offline
                        K-StrK Offline
                        K-Str
                        wrote on last edited by K-Str
                        #13

                        @aha_1980
                        There is a description only in german. But I try to explain :
                        When you set the EN to ON then the input on the DATA pin rises when a new bit is received.
                        the sequence is as follows :

                        1. Set EN to ON
                        2. wait until the DATA goes ON
                        3. read from DATA
                        4. set DATA to OFF
                        5. repeat from 2.

                        Kurt

                        aha_1980A 1 Reply Last reply
                        0
                        • K-StrK K-Str

                          @aha_1980
                          There is a description only in german. But I try to explain :
                          When you set the EN to ON then the input on the DATA pin rises when a new bit is received.
                          the sequence is as follows :

                          1. Set EN to ON
                          2. wait until the DATA goes ON
                          3. read from DATA
                          4. set DATA to OFF
                          5. repeat from 2.

                          Kurt

                          aha_1980A Offline
                          aha_1980A Offline
                          aha_1980
                          Lifetime Qt Champion
                          wrote on last edited by
                          #14

                          @k-str

                          There is a description only in german

                          No problem for me ;)

                          Can you give me a link to that?

                          Qt has to stay free or it will die.

                          1 Reply Last reply
                          0
                          • aha_1980A aha_1980

                            @k-str said in How to use bit operations:

                            The DATA is connectet to pin 13 (GPIO02) as input

                            And where do you get clock from? Or how do you know when a bit starts and ends?

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by
                            #15

                            @aha_1980 said in How to use bit operations:

                            Or how do you know when a bit starts and ends?

                            Priceless :)

                            J.HilkJ 1 Reply Last reply
                            1
                            • JonBJ JonB

                              @aha_1980 said in How to use bit operations:

                              Or how do you know when a bit starts and ends?

                              Priceless :)

                              J.HilkJ Offline
                              J.HilkJ Offline
                              J.Hilk
                              Moderators
                              wrote on last edited by
                              #16

                              @jonb Usually a bit starts when 3 or 5 V are on the bin right  😉


                              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                              Q: What's that?
                              A: It's blue light.
                              Q: What does it do?
                              A: It turns blue.

                              1 Reply Last reply
                              0
                              • K-StrK Offline
                                K-StrK Offline
                                K-Str
                                wrote on last edited by K-Str
                                #17

                                @aha_1980 :
                                yes here some descriptions in german:
                                Descripton in german
                                Its a programm for reading RX868SH.DV and WH1080 weather station as well a description ( see link Datenprotokolle on the end)
                                and here the link to the R868SH-DV module
                                @JonB @J-Hilk :
                                I will check it!
                                Kurt

                                aha_1980A 1 Reply Last reply
                                0
                                • K-StrK K-Str

                                  @aha_1980 :
                                  yes here some descriptions in german:
                                  Descripton in german
                                  Its a programm for reading RX868SH.DV and WH1080 weather station as well a description ( see link Datenprotokolle on the end)
                                  and here the link to the R868SH-DV module
                                  @JonB @J-Hilk :
                                  I will check it!
                                  Kurt

                                  aha_1980A Offline
                                  aha_1980A Offline
                                  aha_1980
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #18

                                  @K-Str

                                  Ok, thanks.

                                  As I expected, this is really implemented by polling a GPIO pin. And it leads to a CPU load of 14% on Raspberry Pi when using 200µs polling interval :-O

                                  Regards

                                  Qt has to stay free or it will die.

                                  1 Reply Last reply
                                  0
                                  • K-StrK Offline
                                    K-StrK Offline
                                    K-Str
                                    wrote on last edited by K-Str
                                    #19

                                    @aha_1980
                                    Thank you for this tip.
                                    This means I have to implement a timer with a interval of 200µs which kicks off reading of one new bit from the RX868SH-DV.
                                    OK I will try it. And then I give you an answer.
                                    But now I got a new problem using timers in QT-Creator. I make a new topic for this.
                                    my new topic for two timer

                                    1 Reply Last reply
                                    0

                                    • Login

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • Users
                                    • Groups
                                    • Search
                                    • Get Qt Extensions
                                    • Unsolved