Thread: bit vs bytes when comparing a file

  1. #1
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329

    bit vs bytes when comparing a file

    I'm sort of drawing a blank on the comments in the following code snippet

    Code:
    test0 = byte1 ^ byte2 /* if identical then bytes are identical */
    test1 = byte1 & byte2 /* these are the bits that are the same */
    I thought that '^' was used to compare bits, not bytes. If test0 is in fact comparing bytes, how would this differ than say comparing bits?

  2. #2
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    The first comment seems to be saying that if the bits in test0 are identical to those in byte1, then byte1 and byte2 are identical. The second seems to be saying that test1 has, turned on, the bits which are equal in byte1 and byte2.

    But going by this interpretation, the comments are on the wrong lines.
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

  3. #3
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    test0 is the result of XORing the two bytes. The result will be all-bits zero if both bytes are identical.
    test1 is the result of logical ANDing. The resulting byte (assuming test1 is a byte) will have the bits that are both turned on in byte1 and byte2 also turned on (and the others turned off).
    The second comment makes sense to me, the first however seems a bit erroneous and not very helpful (I would have commented "if identical, test0 is 0").

  4. #4
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Perhaps I'm overcomplicating the problem, but the the test seems to imply that there could be two equal bytes with different bit patterns. The only thing I could think of would be something like the IEE representation of a float. Does anyone have an example where two bytes would be equal with different bit patterns?

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Get yourself a piece of paper and a pencil. Write two pairs of bit sequences. The first pair should consist of two different sequences, the second pair of two identical sequences. Then perform the two operations as indicated in the code you posted.

    Comments are usually written by people, for people. They're not always clear or even correct, and the compiler does not care about them, only about the code.

  6. #6
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Quote Originally Posted by Overworked_PhD View Post
    Perhaps I'm overcomplicating the problem, but the the test seems to imply that there could be two equal bytes with different bit patterns. The only thing I could think of would be something like the IEE representation of a float. Does anyone have an example where two bytes would be equal with different bit patterns?
    I failed to realized that there is no constraint the relative ordering of the bits in the bytes. Okay, I'm done responding to myself. I'm going to go outside to talk to the ferns.

  7. #7
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    After normalization, an IEEE 754 float's byte pattern would be comparatively equal with an equal float value (except maybe in the case of a dirty zero vs clean zero).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. strcat - cannot convert char to const char
    By ulillillia in forum C Programming
    Replies: 14
    Last Post: 12-07-2006, 10:00 AM
  4. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  5. Reading Certain number of bytes from a file
    By (TNT) in forum Windows Programming
    Replies: 6
    Last Post: 01-14-2002, 08:35 PM