Solved

# What's happening here?

Posted on 2004-09-02
229 Views
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,
}

Tony
0
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 23

Assisted Solution

brettmjohnson earned 100 total points
ID: 11970214
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

TrekkyLeaper earned 100 total points
ID: 11970219
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

ID: 11970223
^ is the bitwise XOR operation

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

0

LVL 11

Assisted Solution

avizit earned 100 total points
ID: 11970230
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

ID: 11970259
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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
###### Suggested Courses
Course of the Month8 days, 13 hours left to enroll