A trivial XOR doubt in a program


 
Thread Tools Search this Thread
Top Forums Programming A trivial XOR doubt in a program
# 1  
Old 10-15-2008
A trivial XOR doubt in a program

Hi,

I am trying to reverse a string using the following program utilizing the Exclusive OR bit operation:

Code:
int main() {
   char str[] = "Quraish";
   char *p = str, temp;
   char *q = str + strlen(str) - 1;
   while ( p != q ) {
      if (*p != *q) {
         *p ^= *q; *q ^= *p; *p ^= *q;
      }
      p++; q--;
   }
   printf("%s \n", str);
}

The above code works perfectly alright, but if I change the line
Code:
*p ^= *q; *q ^= *p; *p ^= *q;

to
Code:
*p ^= *q ^= *p ^= *q;

(I am trying to do swapping inline)
it prints an empty string. Could anyone tell me why this behaviour is?
# 2  
Old 10-15-2008
The short answer - it is an undefined operation. Means that the C standard regards this as garbage, and your compiler was polite enough to produce spaces.
The reason: there are no sequence points in the line between important steps.

A ; character creates a sequence point. So the first version works. This means the compiler can do any of those calculations in any order...

By the way, that 'swap' algorithm in general is a bad idea; it has unsafe properties. You should use a temp variable. It may look cool to you, but that is about it.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

XOR two strings

hi, i am new to shell programming, can u please tell me how to perform XOr operation of two strings. i tried to do xor using ^symbol but this doesnt work. help me with this Thanks (12 Replies)
Discussion started by: anil_uvce
12 Replies

2. FAQ Submission Queue

Analysis in bitwise XOR

The purpose of this article is revealing the unrevealed parts of the bitwise XOR. As we aware, the truth table for the XOR operator is : A B A^B 0 0 0 0 1 1 1 0 1 1 1 0 For example , 1^2 will be calculated as given below: First the operands... (1 Reply)
Discussion started by: pandeesh
1 Replies

3. UNIX for Dummies Questions & Answers

XOR between strings

I am aware of truth table for XOR between binary values . Out of curious in would like to know how XOR works between 2 strings which contain alphabets . For example A ^ B How it works internally? Please help me to understand this Thanks (1 Reply)
Discussion started by: pandeesh
1 Replies

4. Programming

Trivial doubt about C function pointer

Hi, In the below C code, #include <stdio.h> void print() { printf("Hello\n"); } int main() { void (*f)() = (void (*)()) print; f(); (*f)(); } I wonder, how the syntaxes "f()" and "(*f)()" are treated as same without any error? Is this an improvement or ANSI/ISO... (1 Reply)
Discussion started by: royalibrahim
1 Replies

5. Shell Programming and Scripting

Trivial perl doubt about FILE

Hi, In the following perl code: #!/usr/bin/perl -w if (open(FILE, "< in_file")) { while (<FILE>) { chomp($_); if ($_ =~ /patt$/) { my $f = (split(" ", $_)); print "$f\n"; } } close FILE; } Why changing the "FILE" as... (4 Replies)
Discussion started by: royalibrahim
4 Replies

6. Shell Programming and Scripting

Doubt in this trivial awk code

Hi, What is the difference in the following two awk one-liners? awk -F, '{s++} END {if (s == 1 && $4 > "09:10:00") {print $2, $4}}' f1 awk -F, '{s++} s == 1 && $4 > "09:10:00" {print $2, $4}' f1 Even though, all the 2nd column values have duplicate records, the first code does not give any... (4 Replies)
Discussion started by: royalibrahim
4 Replies

7. Shell Programming and Scripting

xor 2 values in ksh?

i have to xor two variables in ksh. how to do that? tia, DN2 (5 Replies)
Discussion started by: DukeNuke2
5 Replies

8. Programming

resetting counter using bitwise XOR

Hi ! How to reset a variable to 0 after a reset value, say 10 using bitwise XOR. For example, int cnt=0; if(cnt<10) cnt++; else cnt = 0; How can we achieve this by using XOR only. thanks, (1 Reply)
Discussion started by: mrgubbala
1 Replies

9. Shell Programming and Scripting

trivial awk question

i posted a reply the other day and needed an answer to this question while i was clarifyiing a few matter.. "how to compare to date variable in string format without having to compare word for word".. my reply was to try to use awk to compare the strings.. I wasn't quite sure if i remembered how... (2 Replies)
Discussion started by: moxxx68
2 Replies
Login or Register to Ask a Question