负数的二进制表示方法

 

原文地址:http://www.cnblogs.com/junsky/archive/2009/08/06/1540727.html

 

今天在看base64编码转换时,既然对负数的二进制表示有些遗忘,在网上找了一下资料,贴出来已备在此遗忘:

假设有一个 int 类型的数,值为5,那么,我们知道它在计算机中表示为:

00000000 00000000 00000000 00000101

5转换成二制是101,不过int类型的数占用4字节(32位),所以前面填了一堆0。

现在想知道,-5在计算机中如何表示?

 

在计算机中,负数以其正值的补码形式表达

什么叫补码呢?这得从原码,反码说起。

 

原码:一个整数,按照绝对值大小转换成的二进制数,称为原码。

比如 00000000 00000000 00000000 00000101 是 5的 原码。

 

反码:将二进制数按位取反,所得的新二进制数称为原二进制数的反码。

取反操作指:原为1,得0;原为0,得1。(1变0; 0变1)

比如:将00000000 00000000 00000000 00000101每一位取反,得11111111 11111111 11111111 11111010。

称:11111111 11111111 11111111 11111010 是 00000000 00000000 00000000 00000101 的反码。

反码是相互的,所以也可称:

11111111 11111111 11111111 11111010 和 00000000 00000000 00000000 00000101 互为反码。

 

补码:反码加1称为补码。

也就是说,要得到一个数的补码,先得到反码,然后将反码加上1,所得数称为补码。

比如:00000000 00000000 00000000 00000101 的反码是:11111111 11111111 11111111 11111010。

那么,补码为:

11111111 11111111 11111111 11111010 + 1 = 11111111 11111111 11111111 11111011


所以,-5 在计算机中表达为:11111111 11111111 11111111 11111011。转换为十六进制:0xFFFFFFFB。


再举一例,我们来看整数-1在计算机中如何表示。

假设这也是一个int类型,那么:


1、先取1的原码:00000000 00000000 00000000 00000001

2、得反码:     11111111 11111111 11111111 11111110

3、得补码:     11111111 11111111 11111111 11111111


可见,-1在计算机里用二进制表达就是全1。16进制为:0xFFFFFF

6的原码是00000110 
6的反码是11111001 
反码+1(补码)表示负数 
11111010 
这就是-6


//==============================================================
//以下是Based64转码,在http传输的时候用得比较多。

 1 /**
 2    This stream filter converts a stream of bytes to their
 3    Base64 encoding.
 4 
 5    Base64 encoding encodes 3 bytes into 4 characters.
 6    |11111122|22223333|33444444|
 7    Each set of 6 bits is encoded according to the
 8    toBase64 map. If the number of input bytes is not
 9    a multiple of 3, then the last group of 4 characters
10    is padded with one or two = signs. Each output line
11    is at most 76 characters.
12 */
13 class Base64OutputStream extends FilterOutputStream
14 {
15    /**
16       Constructs the stream filter
17       @param out the stream to filter
18    */
19    public Base64OutputStream(OutputStream out)
20    {  
21       super(out);
22    }
23 
24    public void write(int c) throws IOException
25    {  
26       inbuf[i] = c;
27       i++;
28       if (i == 3)
29       {  
30          super.write(toBase64[(inbuf[0] & 0xFC) >> 2]);
31          super.write(toBase64[((inbuf[0] & 0x03) << 4) | ((inbuf[1] & 0xF0) >> 4)]);
32          super.write(toBase64[((inbuf[1] & 0x0F) << 2) | ((inbuf[2] & 0xC0) >> 6)]);
33          super.write(toBase64[inbuf[2] & 0x3F]);
34          col += 4;
35          i = 0;
36          if (col >= 76)
37          {  
38             super.write('\n');
39             col = 0;
40          }
41       }
42    }
43 
44    public void flush() throws IOException
45    {  
46       if (i == 1)
47       {  
48          super.write(toBase64[(inbuf[0] & 0xFC) >> 2]);
49          super.write(toBase64[(inbuf[0] & 0x03) << 4]);
50          super.write('=');
51          super.write('=');
52       }
53       else if (i == 2)
54       {  
55          super.write(toBase64[(inbuf[0] & 0xFC) >> 2]);
56          super.write(toBase64[((inbuf[0] & 0x03) << 4) | ((inbuf[1] & 0xF0) >> 4)]);
57          super.write(toBase64[(inbuf[1] & 0x0F) << 2]);
58          super.write('=');
59       }
60    }
61 
62    private static char[] toBase64 =
63    {  
64       'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
65       'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
66       'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
67       'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
68    };
69 
70    private int col = 0;
71    private int i = 0;
72    private int[] inbuf = new int[3];
73 }
74 

  

 

posted @ 2017-09-05 09:08  心有八千里路  阅读(744)  评论(0编辑  收藏  举报