This post has already been read 2808 times!
write a code to reverse a C-Style String - ( C-String means that “abcd” is represented as five characters, including the null character )
EXCLUSIVE DISJUNCTION or EXCLUSIVE OR is a logical operation that outputs TRUE whenever both inputs differ (one is true, the other is false)
The XOR swap is an algorithm that uses the XOR bitwise operation to swap values of distinct variables having the same data type without using a temporary variable.
"Distinct" means that the variables are stored at different memory addresses; the actual values of the variables do not have to be different.
Conventional swapping requires the use of a temporary storage variable.
Using the XOR swap algorithm, however, no temporary storage is needed. The algorithm is as follows :
STEP 1 : X := X XOR Y
STEP 2 : Y := X XOR Y
STEP 3 : X := X XOR Y
EXAMPLE : decimal 5 = 0101 and 3 = 0011
swap x = 5 - and y = 3
STEP 1 : x = x XOR y ----> 0101 XOR 0011 = 0110 -> x
STEP 2 : y = x XOR y ----> 0110 XOR 0011 = 0101 -> y => is now 0101 = 5 (decimal)
STEP 3 : x = x XOR y ----> 0101 XOR 0011 = 0110 -> x => is now 0110 = 3 (decimal)
the values are switched x = 3 and y = 5
the git websource :