您好,登錄后才能下訂單哦!
本文為大家解析了java原碼補碼反碼的關(guān)系,供大家參考,具體內(nèi)容如下
原碼:不管源數(shù)據(jù)是十進(jìn)制還是十六進(jìn)制,統(tǒng)統(tǒng)將數(shù)字轉(zhuǎn)成二進(jìn)制形式
反碼:把原碼的二進(jìn)制統(tǒng)統(tǒng)反過來,0變成1,1變成0
補碼:負(fù)數(shù)的反碼加1,就是負(fù)數(shù)的補碼
例子1:
十進(jìn)制整數(shù) -5,-5的原碼101,-5的反碼1,010,-5的補碼1,011,
所以,-5的反碼是-2,-5的補碼是-3,其中前面的"1,",表示正數(shù)負(fù)數(shù)
例子2:
十六進(jìn)制 -ff9B
原碼 11111111111110011011
反碼 00000000000001100100 十進(jìn)制100
補碼 00000000000001100100 十進(jìn)制101
原來正確的操作流程是,把16進(jìn)制ff9B轉(zhuǎn)成2進(jìn)制,然后直接反碼,再算補碼,就是溫度數(shù)據(jù)
代碼驗證:
package comtest.example.admin.znum; /** * Created by wrs on 2019/6/5,16:25 * projectName: Testz * packageName: comtest.example.admin.znum * 輸入一個真值(整數(shù))求它的原碼,反碼,補碼 */ import java.util.Scanner; public class Test { public static void main(String[] args) { System.out.println("True value--> original code"); System.out.println("Please enter an integer!!!!!"); Scanner sc = new Scanner(System.in); int value = sc.nextInt(); StringBuilder syuan = new StringBuilder(); if (value > 0) { String Bvalue = Integer.toBinaryString(value);//將一個整數(shù)轉(zhuǎn)換成字符串類型的二進(jìn)制數(shù) int n = Bvalue.length(); //二進(jìn)制的數(shù)字的個數(shù)n syuan.append("0,"); syuan.append(Bvalue); System.out.println("Original code " + syuan); //原碼 System.out.println("Complement code " + syuan); //補碼 System.out.println("Inverse code " + syuan); //反碼 // System.out.println("二進(jìn)制數(shù)的n "+n); } else if (value < 0) { int value2 = Math.abs(value); //負(fù)數(shù)的絕對值 int value3 = value2 - 1; //減一求反,求補碼 String Bvalue = Integer.toBinaryString(value2); String BFvalue = Integer.toBinaryString(~value2); int n = Bvalue.length(); String str = BFvalue.substring(BFvalue.length() - n); //截取反碼的后幾個數(shù) syuan.append("1,"); syuan.append(Bvalue); System.out.println("Original code is :" + syuan); System.out.println("Inverse code is :1," + str); String BBvalue = Integer.toBinaryString(~value3); String str3 = BBvalue.substring(BFvalue.length() - n); System.out.println("Complement code is :1," + str3); } else { System.out.println("0 Original code is not only"); System.out.println("[+0]riginal code 00.....0"); System.out.println("[-0]riginal code 10.....0"); System.out.println("--------------------------------------------"); System.out.println("0 Complement code is not only"); System.out.println("[+0]和[-0] Complement code 00.....0"); System.out.println("---------------------------------------------"); System.out.println("0 Inverse code is not only"); System.out.println("[+0]Inverse code00..........0"); System.out.println("[-0]Inverse code11..........1"); } System.out.println("-------------------------------------------------"); } }
運行效果:
------------------------------------------------- True value--> original code Please enter an integer!!!!! -65435 Original code is :1,1111111110011011 Inverse code is :1,0000000001100100 Complement code is :1,0000000001100101 -------------------------------------------------
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。