This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
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?
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
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
Joined: May 31, 2001
Posts: 64
posted
0
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.
Hi, U R right, they don't ask big numbers in bitwise operations mostly. Thanks kanchan
herb slocomb
Ranch Hand
Joined: Feb 12, 2001
Posts: 1479
posted
0
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..
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
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.
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
Joined: May 07, 2001
Posts: 195
posted
0
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 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
Joined: May 30, 2001
Posts: 44
posted
0
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, 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