当前位置:网站首页>Why is the integer type 128 to byte -128
Why is the integer type 128 to byte -128
2022-07-25 22:07:00 【l'amour Хэ рутилин】
public static void main(String[] args) {
short a = 128;
byte b = (byte) a;
System.out.println(b);
}I believe many people know that the result of the above code is -128 , But this is why
Everybody knows short The length of is 16 individual bit position So in int in 128 The binary representation of 0000 0000 1000 0000
java During forced rotation If the long bit is converted to the short bit, the binary value at the end of the long bit that is the same as the short bit is retained , therefore b The binary representation of 1000 0000 but 1000 0000 Convert to decimal to 128, Why is it byte It will be expressed as -128 Well ?
Students who have studied computer know that digital binary in computer The highest bit is the sign bit ,1 Represents a negative value ,0 Represents a positive value . And with complement ( The complement of a positive number is the same as the original code , The complement of negative numbers is based on the original code, and then you add 1.) Is stored in the form of . but 1000 0000 The original code of is still 1000 0000.
Let's understand from two perspectives -128 Why is the complement of 1000 0000.
Understand from the meaning of complement
because :256-128=256+(-128) Complement --( There is only addition in the machine ). Subtraction becomes the addition of complement .
and 256-128=128
therefore 256+(-128) Complement =128
therefore (-128) Complement =256-128
(-128) Complement =128 (1000 0000)
0000 0000 0000 0000 0
1000 0001 1111 1111 -1
1000 0010 1111 1110 -2
1000 0011 1111 1101 -3
... .. Complement constantly -1...
1111 1111 1000 0001 -127
Unable to express 1000 0000 -128
So the rules are 8 In bit binary 1000 0000 Representation bit -128 So when other integers 128 Turn into byte When is -128
边栏推荐
猜你喜欢

6-17漏洞利用-反序列化远程命令执行漏洞

Virtual memory and disk

Redis foundation 2 (notes)

别投了,软件测试岗位饱和了...

成为比开发硬气的测试人,我都经历了什么?

3. Editors (vim)

JMeter websocket interface test

Don't know mock test yet? An article to familiarize you with mock

What have I experienced to become a harder tester than development?

Animation curves are used every day. Can you make one by yourself? After reading this article, you will!
随机推荐
The file cannot be saved (what if the folder is damaged and cannot be read)
Redis是什么?简述它的优缺点
sql语句练习题整理
[fan Tan] in detail: lower control, upward management, upward drawing cake.
开户就可以购买收益在百分之六以上的理财产品了吗
【饭谈】那些看似为公司着想,实际却让人无法理解的事(二:面试时的软素质“眼缘”)
Jmeter---设置代理录制请求
Why does redis choose single thread?
[51Nod1676 无向图同构]无向图哈希[通俗易懂]
突破性思维在测试工作中的应用
JMeter websocket interface test
JSP初识
Jenkins+svn configuration
聚名十年,说出你的故事,百万豪礼等你拿
6-17漏洞利用-反序列化远程命令执行漏洞
All you want to know about interface testing is here
手机端微信发朋友圈功能测试点总结
JS timer and swiper plug-in
dovecot 设置邮箱quota
【汇编语言01】基础知识