Results 1 to 5 of 5

Thread: how to set different bits of a byte

  1. #1
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default how to set different bits of a byte

    I never faced this case to change the bits of one byte. My variable were always bytes, so I always used
    QByteArray to append every hex to it. Now, in the new case, Some variables are one byte, but some other
    variable are only bits, that is I need to set each bit of one byte. I am looking for a nice way to implement this in Qt.
    what do you suggest?

  2. #2
    Join Date
    Sep 2012
    Location
    Iran, Tehran
    Posts
    76
    Thanks
    17
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: how to set different bits of a byte

    Take a look at QBitArray

  3. #3
    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: how to set different bits of a byte

    Manipulating bits in bytes/ints etc. is generic C/C++, not something you need Qt for. It is done with the bitwise OR (|), AND (&) and XOR (^) operators:
    Qt Code:
    1. unsigned char byte;
    2. byte = 0; // set all the bits
    3. byte |= 1; // set LSB (bit 0)
    4. byte |= 128 ; // set MSB (bit 7)
    5. byte |= 1 << 2 ; // set bit 2
    6. byte |= 8 | 16; // set some more bits
    7.  
    8. byte &= ~4; // unset bit 2
    9. byte &= ~(8 | 16); // unset some more bits
    10.  
    11. byte ^= 64; // toggle bit 6
    12. byte ^= 64; // toggle bit 6 back again
    To copy to clipboard, switch view to plain text mode 
    Last edited by ChrisW67; 26th February 2013 at 22:58.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to set different bits of a byte

    I'm wondering if it is safe to use bitfields for this...
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    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: how to set different bits of a byte

    Cannot say I know the standard well enough to know the actual allocated size of a bitfield structure and any bit ordering stipulations; it does seem the entire concept is compiler dependent though. I do know that a bitfield that will not fit in the existing allocation will cause another int (or whetever) to be allocated and a bit gap inserted.

Similar Threads

  1. Replies: 2
    Last Post: 23rd October 2015, 13:29
  2. writing bits
    By timmu in forum Qt Programming
    Replies: 1
    Last Post: 7th January 2013, 11:21
  3. Application 32/64 bits with VS2005/Qt 4.3
    By mourad in forum Installation and Deployment
    Replies: 0
    Last Post: 21st February 2012, 10:39
  4. can't set serial port data bits
    By yyiu002 in forum Qt Programming
    Replies: 6
    Last Post: 23rd June 2010, 22:28
  5. Replies: 3
    Last Post: 19th April 2010, 14:16

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.