Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package zad1a2014;
- import static java.lang.Math.pow;
- /**
- *
- * @author Szymek
- */
- public class Zad1A2014 {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args)
- {
- String xd = "110111010101000011001100101011011"; //10111010 = 186
- System.out.println(liczbaF(xd));
- }
- //0:znak bitowy; cecha: 1, 2, 3, 4, 5, 6, 7, 8; mantysa: 9,10,11...31
- static float liczbaF(String lancuch)
- {
- //System.out.println("Podany łańcuch: " + lancuch);
- int znakLiczby;
- int cecha = 0;
- float mantysa = 0;
- float wartosc;
- znakLiczby = (lancuch.charAt(0)=='1') ? -1 : 1;
- int wykladnik = 7;
- for (int i = 1; i < 9; i++)
- {
- cecha += Integer.parseInt(String.valueOf(lancuch.charAt(i))) * pow(2, wykladnik--);
- }
- //System.out.print("Cecha: " + cecha);
- cecha -= 127;
- //System.out.println(" Cecha-127: " + cecha);
- wykladnik = -1;
- for (int i = 9; i < 32; i++)
- {
- mantysa += Integer.parseInt(String.valueOf(lancuch.charAt(i))) * pow(2, wykladnik--);
- }
- //System.out.print("Mantysa: " + mantysa);
- mantysa += 1;
- //System.out.println(" Mantysa +1: " + mantysa);
- wartosc = Float.parseFloat(String.valueOf(mantysa*pow(2, cecha)));
- return znakLiczby * wartosc;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement