Solved

What's happening here?

Posted on 2004-09-02
5
221 Views
Last Modified: 2010-04-01
In the following snippet, the text is being "encrypted" or something along those lines.  I was hoping someone could tell me exactly what's being done....

char szTest[]={      
                        'T'            ^ ( char) 0x0FF,
                        'E'            ^ ( char) 0x0FF,
                        'S'            ^ ( char) 0x0FF,
                        'T'            ^ ( char) 0x0FF,
                      }

Thanks in advance,
Tony
0
Comment
Question by:fattumsdad
5 Comments
 
LVL 23

Assisted Solution

by:brettmjohnson
brettmjohnson earned 100 total points
Comment Utility
Bit-wise Exclusive Or (XOR) (^) has the following properties:
1 ^ 1 = 0
1 ^ 0 = 1
0 ^ 1 = 1
0 ^ 0 = 0

so that a byte XOR'd with FF (all 1s), basically creates a "negative image"
of the byte - all the 0s become 1s and all all the 1s become 0s.

0
 
LVL 5

Accepted Solution

by:
TrekkyLeaper earned 100 total points
Comment Utility
That is doing a one's complement of the character value. For example:

'T' = 0101100 (in binary)

'T' ^ (char) 0x0FF => 0101100 ^ 11111111 = 1010011
0
 
LVL 11

Expert Comment

by:avizit
Comment Utility
^ is the bitwise XOR operation

your T , E , S, T etc ate xor'ed against 0X0FF

0
 
LVL 11

Assisted Solution

by:avizit
avizit earned 100 total points
Comment Utility
you can get the original back by XOR again with the same

the following prog will demostrate


int main(){
char szTest[]={
        'T'          ^ ( char) 0x0FF,
        'E'          ^ ( char) 0x0FF,
        'S'          ^ ( char) 0x0FF,
        'T'          ^ ( char) 0x0FF,
};

for(int i = 0; i < 4; i ++){
        szTest[i]^= 0x0FF;
}

for(int i = 0; i < 4; i ++){
        cout<<szTest[i]<<endl;
}


  return 0;
}
0
 
LVL 1

Author Comment

by:fattumsdad
Comment Utility
I see!  Thanks to each of you for the help.  I don't like having to choose between numerous good answers, so I'm increasing to 300 pts and giving 100 to each.  Thanks again!

Regards,
Tony
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now