溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

java原碼補碼反碼關系解析

發布時間:2020-10-15 17:49:54 來源:腳本之家 閱讀:176 作者:cf8833 欄目:編程語言

本文為大家解析了java原碼補碼反碼的關系,供大家參考,具體內容如下

原碼:不管源數據是十進制還是十六進制,統統將數字轉成二進制形式

反碼:把原碼的二進制統統反過來,0變成1,1變成0

補碼:負數的反碼加1,就是負數的補碼

例子1:

十進制整數 -5,-5的原碼101,-5的反碼1,010,-5的補碼1,011, 
所以,-5的反碼是-2,-5的補碼是-3,其中前面的"1,",表示正數負數

例子2:

十六進制  -ff9B  
原碼  11111111111110011011
反碼  00000000000001100100    十進制100
補碼  00000000000001100100   十進制101
原來正確的操作流程是,把16進制ff9B轉成2進制,然后直接反碼,再算補碼,就是溫度數據

代碼驗證:

package comtest.example.admin.znum;
 
/**
 * Created by wrs on 2019/6/5,16:25
 * projectName: Testz
 * packageName: comtest.example.admin.znum
 * 輸入一個真值(整數)求它的原碼,反碼,補碼
 */
 
 
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);//將一個整數轉換成字符串類型的二進制數
 int n = Bvalue.length(); //二進制的數字的個數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("二進制數的n "+n);
 } else if (value < 0) {
 int value2 = Math.abs(value); //負數的絕對值
 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); //截取反碼的后幾個數
 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
-------------------------------------------------

 以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女