Thread: Requesting help on problem regarding using "^=" on strings

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    18

    Requesting help on problem regarding using "^=" on strings

    Hi all, I am having trouble understanding the answer for this exercise in KNKING c programming a modern approach book

    make a function
    (d) strrev(s) -- Reverses the characters in s (except the null character); returns s.

    Code:
    char *strrev(char *s)
    {
        char *t = s, *p;
        p = s + strlen(s) - 1;
        while (t < p)
        {
            *t ^= *p;
            *p ^= *t;
            *t ^= *p;
            t++;
            p--;
        }
        return s; }
    
    I understand t is a pointer to the first character in the string, and p is the last pointer. I know that ^ is a bitwise exclusive OR operator, but i have no clue what the first 3 lines inside the while loop do, and how they swap the character around. Everything else I understand.

    Thank you all for any help that can be provided. Cheers

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,658
    Question 20.15c

    Use a temporary variable in your code.
    It's a lot clearer to basically anybody what your intention is.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    866
    XOR has 3 basic uses: To invert bits based in a mask; To get the result of different bits in both operands or as a caryless add.
    This kind of code is a trick to swap values without using a temporary variable, but it isn't a good one. Take a look at this two optimized (by the compiler, using -O2 option) functions f() and g():
    Code:
    ; void f( int *a, int *b )
    ; { int t; t = *a; *a = *b; *b = t; }
    f:
      mov eax,[rdi]
      mov edx,[rsi]
      mov [rdi],edx
      mov [rsi],eax
      ret
    
    ; void g( int *a, int *b )
    ; { *a ^= *b; *b ^= *a; *a ^= *b; }
    g:
      mov eax,[rdi]
      xor eax,[rsi]
      mov [rdi],eax
      xor eax,[rsi]
      mov [rsi],eax
      xor [rdi],eax
      ret

  4. #4
    Registered User
    Join Date
    Oct 2020
    Posts
    18
    Thank you for the responses. Cheers a lot !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A "Simple" Array/String Problem. "Help" ASAP needed
    By AirulFmy in forum C Programming
    Replies: 10
    Last Post: 08-19-2015, 04:30 AM
  2. Replies: 2
    Last Post: 12-08-2014, 08:12 PM
  3. "Apply", "OK" and "CANCEL" strings
    By Joelito in forum Windows Programming
    Replies: 2
    Last Post: 05-12-2006, 04:07 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

Tags for this Thread