/*********************************************************** Shifts.java COMP 111 This applet is intended to illustrate how bitwise operations can be used to reveal the binary representation of a number. ***********************************************************/ // imported packages import java.applet.*; import java.awt.*; // Shifts applet // ----------- public class Shifts extends Applet { int value; Button shiftLeft; Button shiftRight; TextField display; TextField entry; // layout // // +------------------+ // | |entry| | // | ----- | // | __ __ | // | |<<| |>>| | // | -- -- | // | ______________ | // | | display | | // | -------------- | // +------------------+ public void init() { resize(400,300); setLayout(new GridLayout(3,1,5,5)); value = 0; entry = new TextField("", 12); entry.setText(String.valueOf(value)); display = new TextField("", 35); display.setEditable(false); display.setText(toBinaryString(value)); shiftLeft = new Button("<<"); shiftRight = new Button(">>"); Panel p1 = new Panel(); p1.add(entry); Panel p2 = new Panel(); p2.add(shiftLeft); p2.add(shiftRight); Panel p3 = new Panel(); p3.add(display); add(p1); add(p2); add(p3); } // method to convert text in entry field to an integer value private void readVal() { Integer i = Integer.valueOf(entry.getText()); value = i.intValue(); } // update the display to match the entry private void update() { entry.setText(String.valueOf(value)); display.setText(toBinaryString(value)); repaint(); } public boolean action(Event e, Object arg) { if (e.target == shiftLeft) value = value << 1; else if (e.target == shiftRight) value = value >> 1; else readVal(); update(); return true; } // checks if kth bit in (32-bit) binary representation of // integer n is on (i.e., 1) public boolean isKthBitOn(int n, int k) { if (k < 1) return false; else { int i = n & (1 << (k-1)); return (i != 0); } } // convert kth bit in (32-bit) binary representation of // integer n to string ("0" or "1") public String kthBit(int i, int k) { if (isKthBitOn(i, k)) return "1"; else return "0"; } // convert integer i to a String revealing its // (32-bit) binary representation public String toBinaryString(int i) { String s = ""; int k = 1; // bits 1-8 s = kthBit(i,k) + s; k = k + 1; s = kthBit(i,k) + s; k = k + 1; s = kthBit(i,k) + s; k = k + 1; s = kthBit(i,k) + s; k = k + 1; s = kthBit(i,k) + s; k = k + 1; s = kthBit(i,k) + s; k = k + 1; s = kthBit(i,k) + s; k = k + 1; s = kthBit(i,k) + s; k = k + 1; // bits 9-16 s = " " + s; s = kthBit(i,k) + s; k = k + 1; s = kthBit(i,k) + s; k = k + 1; s = kthBit(i,k) + s; k = k + 1; s = kthBit(i,k) + s; k = k + 1; s = kthBit(i,k) + s; k = k + 1; s = kthBit(i,k) + s; k = k + 1; s = kthBit(i,k) + s; k = k + 1; s = kthBit(i,k) + s; k = k + 1; // bits 17-24 s = " " + s; s = kthBit(i,k) + s; k = k + 1; s = kthBit(i,k) + s; k = k + 1; s = kthBit(i,k) + s; k = k + 1; s = kthBit(i,k) + s; k = k + 1; s = kthBit(i,k) + s; k = k + 1; s = kthBit(i,k) + s; k = k + 1; s = kthBit(i,k) + s; k = k + 1; s = kthBit(i,k) + s; k = k + 1; // bits 25-32 s = " " + s; s = kthBit(i,k) + s; k = k + 1; s = kthBit(i,k) + s; k = k + 1; s = kthBit(i,k) + s; k = k + 1; s = kthBit(i,k) + s; k = k + 1; s = kthBit(i,k) + s; k = k + 1; s = kthBit(i,k) + s; k = k + 1; s = kthBit(i,k) + s; k = k + 1; s = kthBit(i,k) + s; return s; } }