Analysis in bitwise XOR


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Answers to Frequently Asked Questions FAQ Submission Queue Analysis in bitwise XOR
# 1  
Old 03-16-2012
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 :
Code:
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 are being converted to their respective binary forms and the xor operation is being performed.
Code:
1== 001
2== 010
1^2==011==3
pandeeswaran@ubuntu:~$ echo $(( 1 ^ 2 ))
3

The interesting finding over here is:
Code:
pandeeswaran@ubuntu:~$ echo $(( 2 ^ 3 ))
1
pandeeswaran@ubuntu:~$ echo $(( 4 ^ 5 ))
1
pandeeswaran@ubuntu:~$ echo $(( 124 ^ 125 ))
1
pandeeswaran@ubuntu:~$ echo $(( 12452 ^ 12453 ))
1
pandeeswaran@ubuntu:~$ echo $(( 124524567878 ^ 124524567879 ))
1
pandeeswaran@ubuntu:~$ echo $(( 121111111111111111111111111111111111111111110 ^ 121111111111111111111111111111111111111111111 ))
1

In general, if the operands are in the form of n ^ (n+1) and also n is an even number, then the result is always 1.
XOR operation between strings:
Code:
pandeeswaran@ubuntu:~$ echo $(( password1 ^ _ ))
0
pandeeswaran@ubuntu:~$ echo $(( A ^ A ))
0
pandeeswaran@ubuntu:~$ echo $(( A ^ B ))
0
pandeeswaran@ubuntu:~$ echo $(( A ^ BS ))
0
pandeeswaran@ubuntu:~$ echo $(( AA ^ BS ))
0
pandeeswaran@ubuntu:~$ echo $(( AAA ^ BAS ))
0
pandeeswaran@ubuntu:~$ echo $(( A1 ^ BAS ))
0
pandeeswaran@ubuntu:~$ echo $(( A1 ^ B1 ))
0

It’s tacit that the xor operation between the strings of arbitrary length is undefined.
But the same in perl is behaving differently:
Code:
pandeeswaran@ubuntu:~$ perl -e ‘print A ^ C;printf “\n”‘

pandeeswaran@ubuntu:~$ perl -e ‘print A ^ B;printf “\n”‘


Code:
A=65= 01000001
B=66= 01000010
—————————-
00000011=3
A=65=01000001
C=67=01000011
—————————
00000010=2

we can easily relate the above with the result we are getting in perl.
But when we encode this result with base64 method, we will get some meaningful text.
In this way it can be used as an encryption technique.
Code:
pandeeswaran@ubuntu:~$ perl -e ‘print A ^ B;printf “\n”‘|base64
Awo=
pandeeswaran@ubuntu:~$ perl -e ‘print Password1 ^ _________;printf “\n”‘|base64
Dz4sLCgwLTtuCg==


# 2  
Old 03-16-2012
You are getting a number of things mixed up together and confused.

Quote:
In general, if the operands are in the form of n ^ (n+1) and also n is an even number, then the result is always 1.
xor can be considered a form of comparison, too. What bit is going to change when you add 1 to an even number? The last bit, of course, the 1 bit. This isn't so mysterious or strange.

Your perl examples don't work because they contain syntax errors, logic errors, and you typed them in MS Word, which destroys all code by inserting smart quotes. But perl does, to my surprise, do what you say:

Code:
perl -e "print 'asdf' ^ 'asdf';" | hexdump -C
00000000  00 00 00 00                                       |....|
00000004

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. 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

3. Emergency UNIX and Linux Support

bitwise and between two 32 bit binaries

Hello All, i have two 16 bit binaries that in two different variables, i want to perform a bitwise AND between the two and store the result in a different variable. can anyone throw some light on doing this in a bourne shell... eg var1= 1110101010101011 ... (8 Replies)
Discussion started by: venu
8 Replies

4. Programming

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: 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;... (1 Reply)
Discussion started by: royalibrahim
1 Replies

5. Programming

bitwise and if

Hi Suppose we have these code lines: #define _IN_USE 0x001 /* set when process slot is in use */ #define _EXITING 0x002 /* set when exit is expected */ #define _REFRESHING 0x004 ... 1 main () { 2 3 unsigned r_flags =_REFRESHING; 4 5 if (r_flag &... (3 Replies)
Discussion started by: Puntino
3 Replies

6. Shell Programming and Scripting

Bitwise negation

I am taking an online course on Unix scripting. The topic is Unix arithmetic operators and the lesson is Logical and bitwise operations. It is not clear how much storage space Unix uses to represent integers that are typed. Bitwise negation caused me to question how many bits are used to... (3 Replies)
Discussion started by: dLloydm
3 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. UNIX for Advanced & Expert Users

bitwise operators

can anybody write a program to divide a number by another number using bitwise operators (9 Replies)
Discussion started by: areef4u
9 Replies
Login or Register to Ask a Question