The other day I was trying out some memory conversions. I vaguely recalled a piece of code that I wrote in my college days.
package com.twisters;
public class MemoryManager {
public static void main(String[] args) {
int oneKB = 1024; /* No change to this line! */
int oneMB = oneKB;
System.out.println("1 MB of memory is :"+oneMB+" bytes.");/* No change to this line! */
}
}
Obviously I think I have missed something. And I am sure I have missed it in line 5. Help me complete this code!!
Rules – pretty simple ones really!
No additional semicolons.
No changes to the lines marked with the comment /* No change to this line! */
No multiplication or division sign to be used in the program.
No additional packages except java.lang – import(ing) is a costly affair!
Make minimum changes to the code – a maximum of 8 characters change would suffice.
Link for the day – http://mindcipher.com/ - A must view site for any puzzle lover.
Got an answer? Do leave it here.
public class MemoryManager {
ReplyDeletepublic static void main(String[] args) {
int oneKB = 1024; /* No change to this line! */
int oneMB = oneKB+1047552;
System.out.println("1 MB of memory is :"+oneMB+" bytes.");/* No change to this line! */
}
}
int oneMB = oneKB+1047552;
ReplyDeleteHope bitwise is accepted:
ReplyDeleteLine 5:
int oneMB = oneKB << 10;
Line 5:
ReplyDeleteint oneMB = oneKB<<10;
The obvious simple change is:
ReplyDeleteint oneMB = oneKB+1047552;
which is kind of stupid but does the job (and goes inline with my previous statement that performance can be improved using hard coded precalculated values.)
I like the following variant much better, however:
int oneMB = oneKB<<10;
(four changes and it still makes sense)
oneKB+1047552;
ReplyDeletepublic class MemoryManager {
ReplyDeletepublic static void main(String[] args) {
int oneKB = 1024; /* Никаких изменений в этой строке! */
int oneMB = oneKB<<10;
System.out.println("1 MB of memory is :"+oneMB+" bytes.");/* Никаких изменений в этой строке! */
}
}
int oneMB = oneKB<<10;
ReplyDeletepublic static void main(String[] args) {
ReplyDeleteint oneKB = 1024; /* No change to this line! */
int oneMB = oneKB<<10;
System.out.println("1 MB of memory is :" + oneMB + " bytes.");/* No change to this line! */
}
:)
public class MemoryManager {
ReplyDeletepublic static void main(String[] args) {
int oneKB = 1024; /* No change to this line! */
int oneMB = oneKB<<10;
System.out.println("1 MB of memory is :"+oneMB+" bytes.");/* No change to this line! */
}
}
int oneMB = oneKB << 10;
ReplyDeleteint oneMB = 1048576;
ReplyDelete