Qt Bit Multiplication



  • 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]


  • Moderators

    @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



  • @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


  • Moderators

    @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;
    


  • @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.



  • 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


  • Moderators

    @J.Hilk said in Qt Bit Multiplication:

    (uint32) oldUint80256;

    You're multiplying with 0



  • @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;
    

  • Qt Champions 2016

    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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.