fork download
  1. // Program that finds the page number and offset when given the page size
  2. // and the virtual address. Assumes that a system has a 32-bit virtual address
  3.  
  4. import java.io.*;
  5.  
  6. class Ideone {
  7. public static final int VIRTUAL_ADDRESS = 32; // 32 bit virtual address
  8.  
  9. public static void main(String[] args) {
  10. if(args.length != 2) {
  11. throw new IllegalArgumentException("Invalid arguments");
  12. }
  13.  
  14. int pageSize = Integer.parseInt(args[0]);
  15. int address = Integer.parseInt(args[1]);
  16.  
  17. if(pageSize < 1024 || pageSize > 16384) {
  18. throw new IllegalArgumentException("Page size is not within the range of " +
  19. "1024 - 16384");
  20. }
  21.  
  22. if((pageSize & (pageSize - 1)) != 0) { // determines if the passed pageSize is a power of 2
  23. throw new IllegalArgumentException("Page size is not a power of 2");
  24. }
  25.  
  26. double virtualAddressSize = Math.pow(2, VIRTUAL_ADDRESS);
  27. double pages = virtualAddressSize / pageSize;
  28. int pageBits = (int) (Math.log(pages) / Math.log(2)); // find how many page bits
  29. int otherBits = VIRTUAL_ADDRESS - pageBits; //Find the other bits
  30.  
  31. int pageNumber = (address >> otherBits); // bit shift to only keep page bits for page number
  32. int offset = (address & (pageSize - 1)); // find the offset
  33.  
  34. System.out.println("The address " + address + " contains:");
  35. System.out.println("page number = " + pageNumber);
  36. System.out.println("offset = " + offset);
  37. }
  38. }
  39.  
Runtime error #stdin #stdout #stderr 0.04s 711168KB
stdin
19886
stdout
Standard output is empty
stderr
Exception in thread "main" java.lang.IllegalArgumentException: Invalid arguments
	at Ideone.main(Main.java:11)