BitShift source, does the shifting, accessed from applet BitShiftap
/* Marcus Green November 1999 BitShift.java demonstration of
the process of the Java Bit shifting operands << >> >>. Part of
Marcus Greens Java Programmer Certification resources to be found at
http://www.software.u-net.com. You can send me email at mail@marcusgreen.co.uk
*/
import java.io.*;
public class BitShift {
private int iShifted;
public static void main(String argv[]){
if (argv.length < 3){
System.out.println("places to shift, number to shift, shift operator (<< >> >>>)");
System.exit(0);
}
int iShiftPlaces=Integer.parseInt(argv[0]); //number of places to shift
int number=Integer.parseInt(argv[1]); //think of a number
String sShiftOp=argv[2]; //<< >> or >>>
BitShift mc=new BitShift();
mc.doShift(iShiftPlaces,number,sShiftOp);
mc.binconv(-2);
System.out.println("go");
DataInputStream din=new DataInputStream(System.in);
try{din.readChar();}//wait for key press before ending
catch (Exception e){System.out.println("Error");}
}//main
public String doShift(int iShiftPlaces, int itoconvert, String sShiftOp){
boolean minus=false;
boolean signed=true;
if(sShiftOp.equals(">>")){
iShifted=itoconvert >> iShiftPlaces;
}else
if(sShiftOp.equals("<<")){
iShifted=itoconvert << iShiftPlaces;
}else
if(sShiftOp.equals(">>>")){
iShifted=itoconvert >>> iShiftPlaces;
signed=false;
}
System.out.println("The Shift opp is " + sShiftOp);
if(iShifted <0){
minus=true;
}
String s=Integer.toBinaryString(iShifted);
int[] bitAr=getBitAr(iShifted);
String temp=StringVer(bitAr);
System.out.println(temp);
int decimalversion=decver(bitAr,minus);
System.out.println("Java says " + iShifted);
System.out.println("I say " + decimalversion);
System.out.println("Chopped");
System.out.println(byteChop(temp));
return byteChop(temp);
}
public int getShifted(){
return iShifted;
}
public int[] getBitAr(int iToConvert){
int[] bitar=new int[32]; //array used to store 1's and 0's 32 bits
String s=Integer.toBinaryString(iToConvert);
int iAr=0;
for (int iString=s.length()-1;iString >=0; iString--){
bitar[iAr]=Integer.parseInt(s.substring(iString,iString+1));
iAr++;
}
return bitar;
}
public String getBitString(int iToConvert){
/*********************************************/
//Convert to a string representing bits
//with gaps to indicate each byte
//This is with no shifting just to
//Show the bit pattern of the original number
/********************************************/
int[] bitAr=new int[32]; //array used to store 1's and 0's 32 bits
bitAr=getBitAr(iToConvert);
String sToString=StringVer(bitAr);
String sToChop=byteChop(sToString);
return sToChop;
}
public String StringVer(int[] iAr){
String sRetVal="";
for(int i=iAr.length-1;i >=0;i--){
sRetVal+=Integer.toString(iAr[i]);
}
return sRetVal;
}
public int decver(int[] bitar, boolean minus){
int decimalver=0;
int multiplier=0;
int add1forminus=0;
int setsign=1;
if (minus==true){
bitar=flipbits(bitar);
add1forminus=1;
setsign=-1;
}
for(int i=0;i < bitar.length; i++){
if(bitar[i]==1){
decimalver +=Math.pow(2,multiplier);
}
multiplier++;
}
decimalver +=add1forminus;
return (decimalver * setsign);
}
public int[] flipbits(int[] bitar){
for(int i=0; i < bitar.length; i++){
if(bitar[i]==0){
bitar[i]=1;
}else{
bitar[i]=0;
}
}
return bitar;
}
public String setFlipBits(String sBitString){
String sRetVal="";
for(int i=0; i < sBitString.length(); i++){
if(sBitString.substring(i,i+1).equals("1")){
sRetVal=sRetVal+"0";
}else
if(sBitString.substring(i,i+1).equals("0")){
sRetVal=sRetVal+"1";
}else
{
sRetVal=sRetVal+" ";
}
}
return sRetVal;
}
public String byteChop(String s){
//Insert space after every eight
//bit to divide into byest
String sRetVal="";
int ilen=s.length();
String sCurChar="";
for(int i=0; i < ilen;i++){
sCurChar=s.substring(i,i+1);
sRetVal=sRetVal + sCurChar;
//Add 1 to give 1 base count not zero
if((i+1) % 8==0){ //Put the spaces between the bytes
sRetVal=sRetVal + " ";
}
}
return sRetVal;
}
public int[] binconv(int intconvert){
int[] bitarray=new int[32];
//Function to show binary version of a decimal number,
//useful for showing bit manipulation etc. This is rather long
//Winded and I created it before discovering the
//static method Integer.toBinaryString method.
int bitposition=0; //index into bitarray
int temp_int; //to keep integer from getting corrupted
for (int i=0; i <32; i++){ //Clear array first
bitarray[i]=0;
}
if(intconvert<0){
//When number is negative convert to positive
//version of twos compliment to allow divide by 2 later
//beacause ints are all signed and we need to subtract the minus
//number from an unsigned int, we use a temporary long so we can
//get access to bit 31.
long temp_long=2147483648L -Math.abs(intconvert);
temp_int=(int) temp_long; //convert back to int
bitarray[31]=1; //and show sign bit as set
}
else{
temp_int=intconvert; //When positive just make a copy
}
while(temp_int > 0){ //convert decimal to binary representation
if((temp_int %2)!=0){
bitarray[bitposition]=1;
}
bitposition++; //go to next bit to calculate
temp_int>>=1; //divide decimal by 2
}
return bitarray;
}
}//class