动手动脑

 1 yuan码fan码bu码

 

程序实现验证

package complement;

import java.util.Scanner;

public class complement
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个(-127)-128的整数a");
        int Originala;
        int Originalb;
        while (true)
        {
            Originala = sc.nextInt();
            if (Originala >= -127 && Originala <= 128)
            {
                System.out.println("a的原码是:" + getBinary(Originala));
                if (Originala > 0)
                {
                    System.out.println("a的反码是:" + getBinary(Originala));
                    System.out.println("a的补码是:" + getBinary(Originala));
                } else
                {
                    System.out.println("a的反码是:" + getReverse(getBinary(Originala)));
                    System.out.println("a的补码是:" + getComplement(getReverse(getBinary(Originala))));
                }

                break;
            } else
            {
                System.out.println("超出范围,请重新输入");
            }
        }
        System.out.println("请输入一个(-127)-128的整数a");
        while (true)
        {
            Originalb = sc.nextInt();
            if (Originalb >= -127 && Originalb <= 128)
            {
                System.out.println("b的原码是:" + getBinary(Originalb));
                if (Originalb > 0)
                {
                    System.out.println("b的反码是:" + getBinary(Originalb));
                    System.out.println("b的补码是:" + getBinary(Originalb));
                } else
                {
                    System.out.println("b的反码是:" + getReverse(getBinary(Originalb)));
                    System.out.println("b的补码是:" + getComplement(getReverse(getBinary(Originalb))));
                }

                break;
            } else
            {
                System.out.println("超出范围,请重新输入");
            }
        }
        String ca=getComplement(getReverse(getBinary(Originala)));
        String cb=getComplement(getReverse(getBinary(Originalb)));
       int [] complementa=new int[8];
       int [] complementb=new int[8];
       for(int i=0;i<8;i++) {
           complementa[i]=Integer.parseInt(ca.charAt(i)+"");
           complementb[i]=Integer.parseInt(cb.charAt(i)+"");
       }
       System.out.println("按位与的结果:");
       System.out.println(getBinary(Originala&Originalb));
       System.out.println("按位异或的结果:");
       System.out.println(getBinary(Originala^Originalb));

    }
    
    
    public static String getBinary(int k)
    {
        String str = "";
        if (k == 0)
            return "00000000";
        else
        {
            int flag = k > 0 ? 1 : 0;
            k = k > 0 ? k : (-k);
            while (k != 0)
            {
                str = k % 2 + str;
                k = k / 2;
            }
            int in = Integer.parseInt(str);
            if (flag == 1)
            {
                str = String.format("%08d", in);
            } else
            {
                str = "1" + String.format("%07d", in);
            }

            return str;
        }
    }

    public static String getReverse(String str)
    {
        String str2 = "";
        for (int i = 1; i < str.length(); i++)
        {
            if (str.charAt(i) == '1')
            {
                str2 = str2 + "0";
            } else if (str.charAt(i) == '0')
            {
                str2 = str2 + "1";
            }
        }
        return "1" + str2;
    }

    public static String getComplement(String str)
    {
        if ("11111111".equals(str))
            return "00000000";
        else
        {
            int index = 0;
            String str2 = "";
            int flag = (Integer.parseInt(str.charAt(7) + "") + 1) == 2 ? 1 : 0;// 1表示进位,0表示不用进位
            if (flag == 1)
            {
                for (int i = str.length() - 1; i >= 0; i--)
                {
                    if (flag == 1)// 需要进位的话 {
                    {

                        if (Integer.parseInt(str.charAt(i) + "") + 1 == 2)
                        {
                            str2 = "0" + str2;
                            flag = 1;
                        } else if (Integer.parseInt(str.charAt(i) + "") + 1 == 1)
                        {
                            str2 = "1" + str2;
                            index = i;
                            break;
                        }
                    }

                }
                str2 = str.substring(0, index) + str2;

            } else
            {
                str2 = str.substring(0, 7) + "1";
            }

            return str2;
        }
    }
}

 

 

2.同名变量的pingbi原则

输出结果

 

 

3.

 byte(8),short(16),int(32), long(64) 浮点型: float(32), double(64)

由精度较高向精度较低的数据类型转换无精度损失,由精度较低向精度较高的数据类型转换会损失精度

4

浮点数在计算机中的存储机制

 

 

5 字符串拼接

 

posted @ 2019-09-19 00:49  派大星球日记  阅读(158)  评论(0编辑  收藏  举报