Thread: Quick question

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    411

    Quick question

    I am working on a TGA image loader, the last bit of an RLE packet determines the type of packet. I see where someone did this to make that determination, but I really dont understand whats going on here.

    Code:
             
    byte packetHeader;
    
    //packetHeader is read in from the file
       
    if ((packetHeader & 128) == 128)  
    {
       //RLE
    }
    else
    {
       //normal
    }
    Im just getting back to coding, and some things im fuzzy on. But byte is the same as unsigned char right?
    Last edited by Eber Kain; 10-20-2005 at 08:00 PM.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    I don't know the answer, but I'd just like to say that the black text is really hard to see on the dark green background on your website.

    ...also I like your 3D Freecell game, it's just a bit buggy, though. You might get better answers in game programming.
    Last edited by SlyMaelstrom; 10-20-2005 at 07:25 PM.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    Quote Originally Posted by SlyMaelstrom
    I don't know the answer, but I'd just like to say that the black text is really hard to see on the dark green background on your website.

    Your browser doesn't support frames? Every browser I have ever used to look at the site has given me this.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Yes, that's what I get in Internet Explorer.

    In Firefox, I'm getting the standard background in that frame, and frames are supported and enabled.

    You should set a background on your other pages (writing.html... etc) so that certain browsers won't carry the main page's background into the frame.
    Last edited by SlyMaelstrom; 10-20-2005 at 07:38 PM.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    Quote Originally Posted by SlyMaelstrom
    Yes, that's what I get in Internet Explorer.

    In Firefox, I'm getting the standard background in that frame, and frames are supported and enabled.

    You should set a background on your other pages (writing.html... etc) so that certain browsers won't carry the main page's background into the frame.
    Ill set that up, thanks for the tip.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The & is a bitwise operator. The value 128 is 10000000 in binary. If you have an unsigned char that you know is 8 bits, and then you know that the "last" bit is the 8th bit, and so if that is on and everything else is off, the value is 10000000, or 128.

    So if you have some set of bits that are on, and you want t check to see if the last bit is on, you use & to combine your set of bits with 10000000. The & returns 1 for a given bit if and only if both sides have a 1 for that bit. So the following are true:
    Code:
      10110001
    & 10101010
    -----------
      10100000
    
      10110001
    & 10000000
    -----------
      10000000
    
      00110001
    & 10000000
    -----------
      00000000
    As you can see by the last two, doing a bitwise & with 10000000 yields 10000000 if the last (leftmost) bit is on, and 00000000 if the last bit is off. Assuming a byte is an unsigned 8 bit integer, that will determine if the bit is set or not.

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    Quote Originally Posted by Daved
    The & is a bitwise operator. The value 128 is 10000000 in binary. If you have an unsigned char that you know is 8 bits, and then you know that the "last" bit is the 8th bit, and so if that is on and everything else is off, the value is 10000000, or 128.

    So if you have some set of bits that are on, and you want t check to see if the last bit is on, you use & to combine your set of bits with 10000000. The & returns 1 for a given bit if and only if both sides have a 1 for that bit. So the following are true:
    Code:
      10110001
    & 10101010
    -----------
      10100000
    
      10110001
    & 10000000
    -----------
      10000000
    
      00110001
    & 10000000
    -----------
      00000000
    As you can see by the last two, doing a bitwise & with 10000000 yields 10000000 if the last (leftmost) bit is on, and 00000000 if the last bit is off. Assuming a byte is an unsigned 8 bit integer, that will determine if the bit is set or not.
    Awsome, thanks a bunch for that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM