Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cs365Homework2;
- public class Tester {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- int b = 0xffffffff;
- String binaryB =Integer.toBinaryString(b);
- System.out.println(Float.intBitsToFloat(b));
- if (binaryB.length()<32)
- binaryB= "0"+binaryB;
- System.out.println(binaryB);
- String bS = binaryB.substring(0, 1);
- String bE = binaryB.substring(1,9);
- String bF = binaryB.substring(9);
- int exponent= Integer.parseInt(bE, 2)-0b1111111;
- System.out.println(exponent);
- System.out.println(bS);
- System.out.println(bE);
- System.out.println(bF);
- System.out.println();
- }
- }
- // NEW CLASS
- package cs365Homework2;
- public class Fp {
- public int add (int a, int b)
- {
- String binaryA=Integer.toString(a,2);
- if (binaryA.length()<32)
- binaryA= "0"+binaryA;
- String binaryB =Integer.toString(b,2);
- if (binaryB.length()<32)
- binaryB= "0"+binaryB;
- String aS = binaryA.substring(0, 1);
- String aE = binaryA.substring(1,9);
- String aF = binaryA.substring(9);
- int exponentA= Integer.parseInt(aE, 2)-0b1111111;//get rid of bias
- String bS = binaryB.substring(0, 1);
- String bE = binaryB.substring(1,9);
- String bF = binaryB.substring(9);
- int exponentB= Integer.parseInt(bE, 2)-0b1111111;//get rid of bias
- if(exponentA>exponentB)//check the difference to know which fraction to adjust
- {
- int difference= exponentA-exponentB;
- if (difference==1)// if the difference is only 1, we just need to shift the 1 over
- {
- bF="1"+bF;
- }
- else
- {
- bF="1"+bF;
- difference--;
- while (difference!=0)
- {
- bF="0"+bF;//keep adding "0" to the front of the string
- difference--;
- }
- }
- }
- return 0;
- }
- public int mul(int a, int b)
- {
- String binaryA=Integer.toString(a,2);
- if (binaryA.length()<32)
- binaryA= "0"+binaryA;
- String binaryB =Integer.toString(b,2);
- if (binaryB.length()<32)
- binaryB= "0"+binaryB;
- String aS = binaryA.substring(0, 1);
- String aE = binaryA.substring(1,9);
- String aF = binaryA.substring(9);
- String bS = binaryB.substring(0, 1);
- String bE = binaryB.substring(1,9);
- String bF = binaryB.substring(9);
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement