home *** CD-ROM | disk | FTP | other *** search
/ Java Certification Exam Guide / McGrawwHill-JavaCertificationExamGuide.iso / pc / Web Links and Code / ans / chap6 / exer0601 / Half.java
Encoding:
Java Source  |  1997-04-19  |  576 b   |  33 lines

  1. class Half {
  2.    int value;
  3.  
  4.    void putVar1(short s) {
  5.       value &= 0xFFFF0000;
  6.       value |= s;
  7.    }
  8.  
  9.    void putVar2(short s) {
  10.       value &= 0x0000FFFF;
  11.       value |= (s << 16);
  12.    }
  13.  
  14.    short getVar1() {
  15.       return (short)(value);
  16.    }
  17.  
  18.    short getVar2() {
  19.       return (short)(value >> 16);
  20.    }
  21. }
  22.  
  23. class Tester {
  24.    public static void main(String[] args) {
  25.       Half h = new Half();
  26.       h.putVar1((short) 100);
  27.       h.putVar2((short) -91);
  28.  
  29.       System.out.println(h.getVar1());
  30.       System.out.println(h.getVar2());
  31.    }
  32. }
  33.