Practical uses of XOR

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cman

    Practical uses of XOR

    Could you point me to the practical uses of XOR in assembly and
    algorithms? I understand XOR to be "true if and only if p is true or q
    is true". Where is this used? I draw a blank on usage.

    Tilak

  • Mark McIntyre

    #2
    Re: Practical uses of XOR

    On 12 Mar 2007 09:14:29 -0700, in comp.lang.c , "cman"
    <tilakb@gmail.c omwrote:
    >Could you point me to the practical uses of XOR in assembly and
    >algorithms? I understand XOR to be "true if and only if p is true or q
    >is true". Where is this used? I draw a blank on usage.
    This isn't a C question. Ask again in comp.programmin g or a maybe
    maths group, and in the meantime, think about how two-way light
    switches work, like the ones in the stairwell of most houses.
    --
    Mark McIntyre

    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan

    Comment

    • Richard Bos

      #3
      Re: Practical uses of XOR

      "cman" <tilakb@gmail.c omwrote:
      Could you point me to the practical uses of XOR in assembly and
      algorithms? I understand XOR to be "true if and only if p is true or q
      is true". Where is this used? I draw a blank on usage.
      The very simplest of applications: to invert a B&W bitmap image, xor
      each byte with ~0.
      For more involved examples, one uses ^ in conjunction (NPI) with & and |
      for reading, setting, and toggling flags within bytes.

      Richard

      Comment

      • osmium

        #4
        Re: Practical uses of XOR

        "cman" writes:
        Could you point me to the practical uses of XOR in assembly and
        algorithms? I understand XOR to be "true if and only if p is true or q
        is true". Where is this used? I draw a blank on usage.
        The XOR is fundamental to cryptographic work. It is also used in binary
        arithmetic.

        http://en.wikipedia.org/wiki/Adder_(electronics)


        Comment

        • santosh

          #5
          Re: Practical uses of XOR


          cman wrote:
          Could you point me to the practical uses of XOR in assembly and
          algorithms? I understand XOR to be "true if and only if p is true or q
          is true". Where is this used? I draw a blank on usage.
          >
          Tilak
          Don't post all your questions to comp.lang.c. This group only deals
          with ISO C. Post non-C questions to appropriate groups. For this one,
          comp.programmin g, alt.lang.asm, comp.lang.asm.x 86, or sci.math might
          be better candidates. The web is also a good resource for such simple
          questions.

          <http://en.wikipedia.or g/wiki/XOR>

          Comment

          • user923005

            #6
            Re: Practical uses of XOR

            On Mar 12, 9:14 am, "cman" <til...@gmail.c omwrote:
            Could you point me to the practical uses of XOR in assembly and
            algorithms? I understand XOR to be "true if and only if p is true or q
            is true". Where is this used? I draw a blank on usage.
            >
            Tilak
            You'll find some in here:


            Comment

            • Keith Thompson

              #7
              Re: Practical uses of XOR

              rlb@hoekstra-uitgeverij.nl (Richard Bos) writes:
              "cman" <tilakb@gmail.c omwrote:
              >Could you point me to the practical uses of XOR in assembly and
              >algorithms? I understand XOR to be "true if and only if p is true or q
              >is true". Where is this used? I draw a blank on usage.
              >
              The very simplest of applications: to invert a B&W bitmap image, xor
              each byte with ~0.
              [...]

              Surely it's simpler to invert the image itself, using the unary "~"
              operator, rather than constructing another bitmap and xor'ing against
              that.

              --
              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
              San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
              "We must do something. This is something. Therefore, we must do this."
              -- Antony Jay and Jonathan Lynn, "Yes Minister"

              Comment

              • Roberto Waltman

                #8
                Re: Practical uses of XOR

                Richard Bos wrote:
                >"cman" <tilakb@gmail.c omwrote:
                >Could you point me to the practical uses of XOR in assembly and
                >algorithms? I understand XOR to be "true if and only if p is true or q
                >is true". Where is this used? I draw a blank on usage.
                >
                >The very simplest of applications: to invert a B&W bitmap image, xor
                >each byte with ~0.
                Too complicated ...

                #include <stdio.h>
                extern delay_until_nex t_second(void);

                void clock_simulator (void)
                {
                int sound = 0;
                while (1)
                {
                printf(sound ? "Tick\n" : "Tock\n");
                delay_until_nex t_second();
                sound ^= 1;
                }
                }

                Comment

                • CBFalconer

                  #9
                  Re: Practical uses of XOR

                  santosh wrote:
                  cman wrote:
                  >
                  >Could you point me to the practical uses of XOR in assembly and
                  >algorithms? I understand XOR to be "true if and only if p is true
                  >or q is true". Where is this used? I draw a blank on usage.
                  >
                  Don't post all your questions to comp.lang.c. This group only deals
                  with ISO C. Post non-C questions to appropriate groups. For this one,
                  comp.programmin g, alt.lang.asm, comp.lang.asm.x 86, or sci.math might
                  be better candidates. The web is also a good resource for such simple
                  questions.
                  However C has an XOR operator, spelled '^'. The OP should just
                  read his C book.

                  --
                  Chuck F (cbfalconer at maineline dot net)
                  Available for consulting/temporary embedded and systems.
                  <http://cbfalconer.home .att.net>



                  --
                  Posted via a free Usenet account from http://www.teranews.com

                  Comment

                  • Keith Thompson

                    #10
                    Re: Practical uses of XOR

                    Roberto Waltman <bookwormish@rw altman.comwrite s:
                    Richard Bos wrote:
                    >>"cman" <tilakb@gmail.c omwrote:
                    >>Could you point me to the practical uses of XOR in assembly and
                    >>algorithms? I understand XOR to be "true if and only if p is true or q
                    >>is true". Where is this used? I draw a blank on usage.
                    >>
                    >>The very simplest of applications: to invert a B&W bitmap image, xor
                    >>each byte with ~0.
                    >
                    Too complicated ...
                    >
                    #include <stdio.h>
                    extern delay_until_nex t_second(void);
                    >
                    void clock_simulator (void)
                    {
                    int sound = 0;
                    while (1)
                    {
                    printf(sound ? "Tick\n" : "Tock\n");
                    delay_until_nex t_second();
                    sound ^= 1;
                    }
                    }
                    Simpler:

                    sound = 1 - sound;

                    Even simpler:

                    sound = !sound;

                    --
                    Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                    San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
                    "We must do something. This is something. Therefore, we must do this."
                    -- Antony Jay and Jonathan Lynn, "Yes Minister"

                    Comment

                    • Ben Pfaff

                      #11
                      Re: Practical uses of XOR

                      Keith Thompson <kst-u@mib.orgwrites :
                      Roberto Waltman <bookwormish@rw altman.comwrite s:
                      > #include <stdio.h>
                      > extern delay_until_nex t_second(void);
                      >>
                      > void clock_simulator (void)
                      > {
                      > int sound = 0;
                      > while (1)
                      > {
                      > printf(sound ? "Tick\n" : "Tock\n");
                      > delay_until_nex t_second();
                      > sound ^= 1;
                      > }
                      > }
                      >
                      Simpler:
                      >
                      sound = 1 - sound;
                      >
                      Even simpler:
                      >
                      sound = !sound;
                      I'd probably write it like this (modulo coding style):

                      void clock_simulator (void)
                      {
                      while (1)
                      {
                      printf("Tock\n" );
                      delay_until_nex t_second();

                      printf("Tick\n" );
                      delay_until_nex t_second();
                      }
                      }
                      --
                      int main(void){char p[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
                      \n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int putchar(\
                      );while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof p-1;putchar(p[i]\
                      );}return 0;}

                      Comment

                      • =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=

                        #12
                        Re: Practical uses of XOR

                        On Mar 12, 9:31 pm, Roberto Waltman <bookworm...@rw altman.comwrote :
                        Richard Bos wrote:
                        "cman" <til...@gmail.c omwrote:
                        Could you point me to the practical uses of XOR in assembly and
                        algorithms? I understand XOR to be "true if and only if p is true or q
                        is true". Where is this used? I draw a blank on usage.
                        >
                        The very simplest of applications: to invert a B&W bitmap image, xor
                        each byte with ~0.
                        >
                        Too complicated ...
                        >
                        #include <stdio.h>
                        extern delay_until_nex t_second(void);
                        >
                        void clock_simulator (void)
                        {
                        int sound = 0;
                        while (1)
                        {
                        printf(sound ? "Tick\n" : "Tock\n");
                        delay_until_nex t_second();
                        sound ^= 1;
                        Why an exclusive or with 1? Why not 8, or 42, or 197? And why not
                        simply

                        sound = !sound;

                        ? This doesn't seem to me an obvious use of xor... which could just be
                        me, of course.
                        }
                        }

                        Comment

                        • Beej Jorgensen

                          #13
                          Re: Practical uses of XOR

                          Keith Thompson <kst-u@mib.orgwrote:
                          >Surely it's simpler to invert the image itself, using the unary "~"
                          >operator, rather than constructing another bitmap and xor'ing against
                          >that.
                          <OT>
                          Yes, but probably what he was getting at was more along the lines of the
                          famous Atari (was it?) XOR patent for drawing the cursor, namely, that
                          you can "stamp" arbitrary data on other data, and remove it again with
                          XOR, since:

                          (x ^ y) ^ y == x

                          (I'm sure you already knew this, Keith; this post is for posterity.)

                          Cursor on:

                          ....XXX.. XXXXXXXX XXX...XX
                          ...XX.XX. XXXXXXXX XX..X..X
                          ..XX...XX XXXXXXXX X..XXX..
                          ..XXXXXXX XXXXXXXX X.......
                          ..XX...XX XOR XXXXXXXX == X..XXX..
                          ..XX...XX XXXXXXXX X..XXX..
                          ..XX...XX XXXXXXXX X..XXX..
                          ......... XXXXXXXX XXXXXXXX

                          Then cursor off:

                          XXX...XX XXXXXXXX ...XXX..
                          XX..X..X XXXXXXXX ..XX.XX.
                          X..XXX.. XXXXXXXX .XX...XX
                          X....... XXXXXXXX .XXXXXXX
                          X..XXX.. XOR XXXXXXXX == .XX...XX
                          X..XXX.. XXXXXXXX .XX...XX
                          X..XXX.. XXXXXXXX .XX...XX
                          XXXXXXXX XXXXXXXX ........

                          Of course, the cursor could be anything--not just a solid block--and the
                          character beneath would be restored to its original state when the
                          cursor was turned off.

                          IIRC, old versions of Windows would draw the window frame in XOR mode
                          when you were moving or resizing it. Ugly but effective.

                          </OT>

                          -Beej, still looking for that elusive XNOT operation.

                          Comment

                          • mensanator@aol.com

                            #14
                            Re: Practical uses of XOR

                            On Mar 12, 11:14 am, "cman" <til...@gmail.c omwrote:
                            Could you point me to the practical uses of XOR in assembly and
                            algorithms?
                            Hamming Distance = popcount(a^b)
                            I understand XOR to be "true if and only if p is true or q
                            is true". Where is this used? I draw a blank on usage.
                            >
                            Tilak

                            Comment

                            • Coos Haak

                              #15
                              Re: Practical uses of XOR

                              Op Tue, 13 Mar 2007 00:21:51 +0100 (CET) schreef Beej Jorgensen:

                              <snip>
                              -Beej, still looking for that elusive XNOT operation.
                              <OTLike this?
                              XNOR is the connective in logic corresponding to the exclusive nor operation. A XNOR B is equivalent to (A ^ B) v (!A ^ !B), where ^ denotes AND, v denotes OR, and !A denotes NOT. The circuit diagram symbol for an XNOR gate is illustrated above, and the XNOR truth table is given below for two arguments. For two arguments, "A XNOR B" is identical to "A iff B" and "A is equivalent to B" (A=B). A B A XNOR B T T T T F F F T F F F T

                              </OT>
                              --
                              Coos

                              Comment

                              Working...