XOR operator (^)

This is a discussion on XOR operator (^) within the C++ Programming forums, part of the General Programming Boards category; XOR operator (^) So what is it? The same as 8^3 == 8*8*8 == 512 ? And what is the ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    160

    XOR operator (^)

    XOR operator (^)

    So what is it? The same as 8^3 == 8*8*8 == 512 ?
    And what is the storry about XOR. Why is a class needed.
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Xor is exclusive or. This is not the same as 4 raised to the 3 power. Say you want to xor the number 4 with the number 3. This is how it would look (the numbers are converted into binary)

    0000 0100
    xor
    0000 0011
    ---------------
    0000 0111 (7 in decimal form)


    Basically, it is a comparison of bits. If both bits are 0 or 1 in the same spot, then the resulting bit for that spot will be 0. If only 1 of the bits are 1 and the other is 0, the resulting bit for that spot is 1.

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Does this kinda thing get used much? I know bitwise operation are fast, but in practice do they ever really get used?
    Couldn't think of anything interesting, cool or funny - sorry.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >but in practice do they ever really get used?
    In C, yes, quite a bit. In C++, not as much but for low level programming bit-wise operators are wonderful.

    -Prelude
    My best code is written with the delete key.

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    >I know bitwise operation are fast, but in practice do they ever really get used?

    Yes, as Prelude mentioned, in low level applications, they are very useful, since in such applications you're working a lot with individual bits. Think about datacommunication. You receive telegram consisting of several bytes from a certain device, in such telegrams groups of bits and also individual bits can have a meaning.

    But also think about encryption. The XOR is the base for one of the strongest, if not the strongest, encryption techniques, which is the One-Time-Pad. This is based on the property of XOR that:

    plain_message ^ key = encrypted_message
    encrypted_message ^ key = plain_message

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    The link I posted above details XOR encryption, along with strengths and weaknesses.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  8. #8
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    I've read the site and would like to add something.

    For most people's purposes, a key like "(Ta5!34hEf$" will suffice, but if you need extremely secure encryption, you can take the encrypted text, and XOR it with another key, or you could use a file's contents for the key.
    XOR encryption will be uncrackable if the key is as large or larger as the source. This is because then each key is a possible valid key and therefore the encrypted data is uncrackable.

  9. #9
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >XOR encryption will be uncrackable if the key is as large or larger as the source. This is because then each key is a possible valid key and therefore the encrypted data is uncrackable.

    I've edited the article to include this, and given you credit.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

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