Results 1 to 4 of 4

Thread: access to bits of a char variable

  1. #1
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default access to bits of a char variable

    hi, there is a char variable. i need to access to the bits of that. i think i can do this by QBitArray class. please help me in this way. i searched this but saw no example.
    thanks for any help

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: access to bits of a char variable

    You can also just use the bit maipulation operators, such as & and |

    Cheers,
    _

  3. #3
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: access to bits of a char variable

    Quote Originally Posted by anda_skoa View Post
    You can also just use the bit maipulation operators, such as & and |

    Cheers,
    _
    thanks, can you give an example how i can access to bits of an unsigned char variables by QBitArray class? is this code true?
    Qt Code:
    1. unsigned char un;
    2. QBitArray bits(8);
    3. bits = (QBitArray) un;
    To copy to clipboard, switch view to plain text mode 

    thanks fore more help

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: access to bits of a char variable

    QBitArray is a device for managing an arbitrary length list of single-bit flags, not general bit manipulations. There is no obvious way to construct a QBitArray from a preexisting integer that does not involve solving the bit access problem manually first.

    Qt Code:
    1. char c = 0xaa; // 1010 1010
    2. for (int i = 0; i < 8; ++i)
    3. qDebug() << "Bit" << i << ":" << ((c >> i) & 0x01);
    4.  
    5. char bit34 = (c & 0x18); // you may want to right shift these >> 3
    6. // clear bits 3 and 4
    7. c = (c & ~0x18);
    8. // set bits 3 and 4
    9. c = (c | 0x18);
    To copy to clipboard, switch view to plain text mode 
    Last edited by ChrisW67; 27th December 2015 at 21:03.

  5. The following user says thank you to ChrisW67 for this useful post:

    Alex22 (28th December 2015)

Similar Threads

  1. Periodically access QML variable from C++
    By mekarim in forum Newbie
    Replies: 4
    Last Post: 24th November 2015, 11:45
  2. Problem in storing hex data to char variable
    By prasad1001 in forum Newbie
    Replies: 1
    Last Post: 7th April 2014, 07:16
  3. Access variable using its name as QString
    By homerun4711 in forum Newbie
    Replies: 3
    Last Post: 22nd December 2010, 09:11
  4. QtScript access variable from C++
    By bunjee in forum Qt Programming
    Replies: 2
    Last Post: 16th January 2009, 23:51
  5. main.cpp variable access question
    By MarkoSan in forum Qt Programming
    Replies: 10
    Last Post: 10th March 2008, 20:48

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.