Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt Bit Multiplication
Qt 6.11 is out! See what's new in the release blog

Qt Bit Multiplication

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 5 Posters 3.7k 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.
  • A Offline
    A Offline
    akshay123
    wrote on last edited by kshegunov
    #1

    Hi all,
    i have a XML file

    <First_elem>
    <class  name="BIT_CLASS1"  value="0x05" params="{' **bitLo ': 0**  ,' **bitHi '3: ** }"   />
    <class  name="BIT_CLASS2"  value="0x06" params="{' **bitLo ': 4**  ,' **bitHi '8: ** }"   />
    <class  name="BIT_CLASS3"  value="0x10" params="{' **bitLo ': 9**  ,' **bitHi '13: ** }"   />
    <class  name="BIT_CLASS4"  value="0x01" params="{' **bitLo ': 13**  ,' **bitHi '16: ** }"   />
    </First_elem>
    
    <second_elem>
    <class  name="BIT_CLASS5"  value="0x05" params="{' **bitLo ': 17**  ,' **bitHi '23: ** }"   />
    <class  name="BIT_CLASS6"  value="0x06" params="{' **bitLo ': 23**  ,' **bitHi '26: ** }"   />
    <class  name="BIT_CLASS7"  value="0x10" params="{' **bitLo ': 26**  ,' **bitHi '30: ** }"   />
    <class  name="BIT_CLASS8"  value="0x01" params="{' **bitLo ': 31**  ,' **bitHi '32: ** }"   />
    </second _elem>
    

    i need to parse all the elements and need to push the data into a vector of int32. The final contents should look like this .
    101 0110 01000 0001 0000101 110 10000 1

    i am using QDom for parsing . Now i will extract each value from class as a string and will convert that string to int . but i am not getting how to set each bit at the particular position in the int and use the same int variable to set the next value

    Example :
    int final_value ;
    final_value = .. (extract the first element from the first class and set the value in final_value);
    final_value = .. (extract the second element from the second class and set the value in final_value)
    final_value = .. (extract the third element from the third class and set the value in final_value)

    Could some one please help me to how to do this ?

    . so that i can get a final_value like this 101 0110 01000 0001 0000101 110 10000 1

    Thanks

    [Added code tags ~kshegunov]

    jsulmJ kshegunovK 2 Replies Last reply
    0
    • A akshay123

      Hi all,
      i have a XML file

      <First_elem>
      <class  name="BIT_CLASS1"  value="0x05" params="{' **bitLo ': 0**  ,' **bitHi '3: ** }"   />
      <class  name="BIT_CLASS2"  value="0x06" params="{' **bitLo ': 4**  ,' **bitHi '8: ** }"   />
      <class  name="BIT_CLASS3"  value="0x10" params="{' **bitLo ': 9**  ,' **bitHi '13: ** }"   />
      <class  name="BIT_CLASS4"  value="0x01" params="{' **bitLo ': 13**  ,' **bitHi '16: ** }"   />
      </First_elem>
      
      <second_elem>
      <class  name="BIT_CLASS5"  value="0x05" params="{' **bitLo ': 17**  ,' **bitHi '23: ** }"   />
      <class  name="BIT_CLASS6"  value="0x06" params="{' **bitLo ': 23**  ,' **bitHi '26: ** }"   />
      <class  name="BIT_CLASS7"  value="0x10" params="{' **bitLo ': 26**  ,' **bitHi '30: ** }"   />
      <class  name="BIT_CLASS8"  value="0x01" params="{' **bitLo ': 31**  ,' **bitHi '32: ** }"   />
      </second _elem>
      

      i need to parse all the elements and need to push the data into a vector of int32. The final contents should look like this .
      101 0110 01000 0001 0000101 110 10000 1

      i am using QDom for parsing . Now i will extract each value from class as a string and will convert that string to int . but i am not getting how to set each bit at the particular position in the int and use the same int variable to set the next value

      Example :
      int final_value ;
      final_value = .. (extract the first element from the first class and set the value in final_value);
      final_value = .. (extract the second element from the second class and set the value in final_value)
      final_value = .. (extract the third element from the third class and set the value in final_value)

      Could some one please help me to how to do this ?

      . so that i can get a final_value like this 101 0110 01000 0001 0000101 110 10000 1

      Thanks

      [Added code tags ~kshegunov]

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

      @akshay123 No need for Qt here, plain old C/C++ provides everything you need.
      Do you know bit operators in C/C++ (& | ^ <<)?
      Lets say you want to set bit 3 in an int:

      int number = 1;
      number |= 1<<2;
      

      <<2 - this shifts 1 by 2 positions to the left. Before shifting it is: 00000001, after shifting: 00000100
      Then you do a bit wise or: 00000001 | 00000100 = 00000101

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

      1 Reply Last reply
      2
      • A Offline
        A Offline
        akshay123
        wrote on last edited by
        #3

        @jsulm Yes i know the bit operation . What my point was that . if i had variables like this

        uint8 = "00000010"
        uint8="00000100"
        uint8 = "00001100"
        uint8 = "00001110"

        how do i combine all these values into one variable uint32

        so that final variable looks like this uint32 = 00000010000001000000110000001110

        jsulm thanks for your reply

        jsulmJ J.HilkJ 2 Replies Last reply
        0
        • A akshay123

          @jsulm Yes i know the bit operation . What my point was that . if i had variables like this

          uint8 = "00000010"
          uint8="00000100"
          uint8 = "00001100"
          uint8 = "00001110"

          how do i combine all these values into one variable uint32

          so that final variable looks like this uint32 = 00000010000001000000110000001110

          jsulm thanks for your reply

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

          @akshay123 If you know them then you should know how to do it:

          uint8  int4 = "00000010"
          uint8  int3="00000100"
          uint8  int2 = "00001100"
          uint8 int1 = "00001110"
          
          int result = 0;
          result |= int1;
          result |= int2<<8;
          result |= int3<<16;
          result |= int4<<24;
          

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

          1 Reply Last reply
          0
          • A akshay123

            @jsulm Yes i know the bit operation . What my point was that . if i had variables like this

            uint8 = "00000010"
            uint8="00000100"
            uint8 = "00001100"
            uint8 = "00001110"

            how do i combine all these values into one variable uint32

            so that final variable looks like this uint32 = 00000010000001000000110000001110

            jsulm thanks for your reply

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

            @akshay123

            There are multiple ways of doing this, the beauty of programming.

            I would propbably cast the uint8 as uint32

            uint32 newV = (uint32)oldUint8
            

            And multiply it with a multiple of 2^8

            (uint32) oldUint8*0*256;
            (uint32) oldUnit8*1*256
            ...
            

            And in the End, add them together.


            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.

            jsulmJ 1 Reply Last reply
            0
            • dheerendraD Offline
              dheerendraD Offline
              dheerendra
              Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
              wrote on last edited by
              #6

              Akshay as @jsulm said just refer bitwise operators, left shift, right shift operators. Google hit should give you many links to explain this. If u r student your lecture can also help you

              Dheerendra
              @Community Service
              Certified Qt Specialist
              https://www.pthinks.com

              1 Reply Last reply
              4
              • J.HilkJ J.Hilk

                @akshay123

                There are multiple ways of doing this, the beauty of programming.

                I would propbably cast the uint8 as uint32

                uint32 newV = (uint32)oldUint8
                

                And multiply it with a multiple of 2^8

                (uint32) oldUint8*0*256;
                (uint32) oldUnit8*1*256
                ...
                

                And in the End, add them together.

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

                @J.Hilk said in Qt Bit Multiplication:

                (uint32) oldUint80256;

                You're multiplying with 0

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

                J.HilkJ 1 Reply Last reply
                0
                • jsulmJ jsulm

                  @J.Hilk said in Qt Bit Multiplication:

                  (uint32) oldUint80256;

                  You're multiplying with 0

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

                  @jsulm

                  Ha, you're of course correct, thats wrong, what I tried to illustrate was, that the uint8 that gets to be placed on the first 8 bit does not have to be manipulated.

                  Sometimes my mind makes weird association.

                  uint32 newInt = (uint32) oldInt1 + (uint32) oldInt2*1*256 + (uint32) oldInt3*2*256 + (uint32) oldInt4*3*256;
                  

                  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
                  • A akshay123

                    Hi all,
                    i have a XML file

                    <First_elem>
                    <class  name="BIT_CLASS1"  value="0x05" params="{' **bitLo ': 0**  ,' **bitHi '3: ** }"   />
                    <class  name="BIT_CLASS2"  value="0x06" params="{' **bitLo ': 4**  ,' **bitHi '8: ** }"   />
                    <class  name="BIT_CLASS3"  value="0x10" params="{' **bitLo ': 9**  ,' **bitHi '13: ** }"   />
                    <class  name="BIT_CLASS4"  value="0x01" params="{' **bitLo ': 13**  ,' **bitHi '16: ** }"   />
                    </First_elem>
                    
                    <second_elem>
                    <class  name="BIT_CLASS5"  value="0x05" params="{' **bitLo ': 17**  ,' **bitHi '23: ** }"   />
                    <class  name="BIT_CLASS6"  value="0x06" params="{' **bitLo ': 23**  ,' **bitHi '26: ** }"   />
                    <class  name="BIT_CLASS7"  value="0x10" params="{' **bitLo ': 26**  ,' **bitHi '30: ** }"   />
                    <class  name="BIT_CLASS8"  value="0x01" params="{' **bitLo ': 31**  ,' **bitHi '32: ** }"   />
                    </second _elem>
                    

                    i need to parse all the elements and need to push the data into a vector of int32. The final contents should look like this .
                    101 0110 01000 0001 0000101 110 10000 1

                    i am using QDom for parsing . Now i will extract each value from class as a string and will convert that string to int . but i am not getting how to set each bit at the particular position in the int and use the same int variable to set the next value

                    Example :
                    int final_value ;
                    final_value = .. (extract the first element from the first class and set the value in final_value);
                    final_value = .. (extract the second element from the second class and set the value in final_value)
                    final_value = .. (extract the third element from the third class and set the value in final_value)

                    Could some one please help me to how to do this ?

                    . so that i can get a final_value like this 101 0110 01000 0001 0000101 110 10000 1

                    Thanks

                    [Added code tags ~kshegunov]

                    kshegunovK Offline
                    kshegunovK Offline
                    kshegunov
                    Moderators
                    wrote on last edited by kshegunov
                    #9

                    Yet another weird way of doing it (requires C99 compliance for unions):

                    union  {
                        qint32 i32bit;
                        struct  {
                            qint8 byte1, byte2, byte3, byte4;   
                        } bytes;
                    } data;
                    
                    data.bytes.byte1 = 0b00000010;
                    data.bytes.byte2 = 0b00000100;
                    data.bytes.byte3 = 0b00001100;
                    data.bytes.byte4 = 0b00001110;
                    
                    qint32 result = data.i32bit;
                    

                    Warning: Code doesn't take into account endianness.

                    Read and abide by the Qt Code of Conduct

                    1 Reply Last reply
                    1

                    • Login

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