Another brainfart: Question regarding masking

This is a discussion on Another brainfart: Question regarding masking within the C Programming forums, part of the General Programming Boards category; I have a question regarding some "borrowed" code: Code: #define ASIZE sizeof(int); unsigned int uival; unsigned char bytes[ASIZE]; int i; ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195

    Another brainfart: Question regarding masking

    I have a question regarding some "borrowed" code:

    Code:
    #define ASIZE sizeof(int); 
    
    
       unsigned int  uival; 
       unsigned char bytes[ASIZE]; 
       int           i; 
       ... 
       for (uival = 0, i = 0; i < ASIZE; i++) { 
          uival = 256 * uival + (bytes[i] & 0xff); 
       }
    How does the 0xff ensure that only 8 bits per byte are used? I'm not making the connection here. Can someone break this down in terms of "C for 5th graders".

    Thanks
    Last edited by cdalten; 02-11-2006 at 11:56 AM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    0xFF is a value with the lower 8 bits set. ANDing with this value leaves only these lower 8 bits.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Okay, at the risk of being told I can google this, what the heck is bit sets and more to the point. What happens to the upper 8 bits?

  4. #4
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Wait, I think I got it

    0xff in binary is 11111111

    now say I have ike the number 5.

    then AND 0xff and 5 is

    11111111
    00000101

    00000101

    or 5

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    I still don't get what happens to the upper 8 bits. Are they still there?

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    ANDing with zero clears a bit. So if bits 8 and above are zero and you and with anything, bits 8 and higher in the result will be cleared.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    I'll start off with an example:
    Code:
      1001 1010 0111 1100
    & 0000 0000 1111 1111
    ---------------------
      0000 0000 0111 1100
    If any operand in an AND operation is 0 then the result is 0,
    0 & 1 = 0
    0 & 0 = 0
    Which means that the zeroed part of the mask will be zeroed in the result too.

    If any operand in an AND operation is 1 then the result is that of the other operand,
    1 & 1 = 1
    1 & 0 = 0 (one operand was 0 so result is 0 here)
    So the part of the mask with ones will have the same value as the corresponding positions of the variable that was operated on.

  9. #9
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    And now I got it. Thanks.

  10. #10
    Sys.os_type="Unix";;
    Join Date
    Aug 2005
    Posts
    52
    You can check out this website as well

    http://www.gamedev.net/reference/art....asp?the_id=69

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21