# bitwise ^ on Exam?

frank davis
Ranch Hand
Posts: 1479
I had several questions on the new Sybex e-trainer software
that were like this :
class f{
static public void main(String arg[])
{
int i = 345;
for (int n = 1; n <=4; n++)
i^= 543;
System.out.println(i);
}
}
Are these realistic type questions? The only way I see to solve it is convert i and 543 to binary first, but that's way too time time consuming. Is there a short cut and/or are these type questions unrealistic?

Marcellus Tryk
Ranch Hand
Posts: 64
You don't have to do any conversion to binary.
Notice that (a ^ b) ^ b = a
Since there are an even number or ^ operations in the loop, the answer is 345.
- Tod

V Srinivasan
Ranch Hand
Posts: 99
Dear Friends,
These type of questions definitely does binary conversions. I have no clue about shortcut. All the time you will not get ans.345, just change the loop and see the result.
Let us hope somebody posts a shortcut for these type of problem.
Thanks & Regards,
V. Srinivasan

Marcellus Tryk
Ranch Hand
Posts: 64
I believe the point of this question is that you can calculate the result without performing any binary conversion. I haven't come across too many examples where you have to convert a number like 345 to binary. Typically I see hex constants which are much easier to convert - or numbers like 129 that can be converted quickly.

kanchan chaudhary
Greenhorn
Posts: 28
Hi,
U R right, they don't ask big numbers in bitwise operations mostly.
Thanks
kanchan

frank davis
Ranch Hand
Posts: 1479
Thanks to all for the replies, and for the shortcut for even numbered ^s. I think I agree with the last comments. It doesn't make sense to ask questions that would require a lot of time to convert things to binary.
Based on what I have seen of the new Sybex "e-trainer" software by the RHE authors, I have to reccommend NOT buying it. It's basically the same as the book - same exact material, same questions, except it has more typos than the book. I've only gone through the first 2 Chapters but it seems exactly the same as the RHE book so far. My advice is if you have the RHE book, don't waste another \$79 on this software..

michelle hou
Greenhorn
Posts: 4
I still don't get it. Can someone explain to me.
Why this source file compile and run 345
but if I erase line //for (int n = 1; n <=4; n++)
will output like answer at first I guess, 838
Thanks

Scott Appleton
Ranch Hand
Posts: 195
Michelle, the way the code is set up ensures that the loop will execute exactly 4 times. No matter what number you initialize the variable i to, every second iteration through the loop, the ^ operator will return i to its original value. Since the number of loops is even, the final value will always be the same as the original value.
In this program, you could replace the statement "i^543" with "i^7" or "i^4057" and the final value would still be 345.

michelle hou
Greenhorn
Posts: 4
Thanks, Scott.
I got it now.

bill
Greenhorn
Posts: 4
Hi all,
I think that 345 is not right answer.
The fact is: 345 --------> 0101011001
543 --------> 1000011111
-----------------------------------
345^543 -----> 1101000110 --------> 838
(a^b)^b=a is correct, but here a=345^543, can't get the result of 345.

Scott Appleton
Ranch Hand
Posts: 195
Bill, you are only processing the i^= 543 statement once in your example, which returns 838. Process the loop a second time (838^=543) and you will return to 345.
Compile the code and check it out for yourself. If you vary the loop structure to iterate an odd number of times, you will get 838. An even number of iterations yields a final result of 345.

Trevor Green
Ranch Hand
Posts: 44
Erm, Scott, aren't you processing it an odd number of times?
In the loop, n is initialized to 1 and continues to 3. (It goes round three times, no?)

Avinash Rai
Ranch Hand
Posts: 40
Trevor
In the loop n is initialized to 1 and continues to 4. ( n <=4)
So even number of iterations.
Avinash
Originally posted by Trevor Green:
Erm, Scott, aren't you processing it an odd number of times?
In the loop, n is initialized to 1 and continues to 3. (It goes round three times, no?)

Trevor Green
Ranch Hand
Posts: 44
Good point. Sorry. I really didn't read the question. Thought it said n<4.
I seem to be making a habbit of not reading the question fully. Are there any common pitfalls that crop up which require a keen eye so you don't just take the first answer. Any hints?

bill
Greenhorn
Posts: 4
I'm wrong.
Scott is right. Thanks to Scott.

bill
Greenhorn
Posts: 4
I'm wrong.
Scott is right. Thanks to Scott.

Jane Griscti
Ranch Hand
Posts: 3141
Bill,
Please read the JavaRanch Name Policy and re-register using a name that complies with the rules.
Thanks for you cooperation.
------------------
Jane Griscti
Sun Certified Programmer for the Javaï¿½ 2 Platform