当前位置:网站首页>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
边栏推荐
猜你喜欢

少儿编程 电子学会图形化编程等级考试Scratch一级真题解析(判断题)2022年6月

The dragon lizard exhibition area plays a new trick this time. Let's see whose DNA moved?

如何将一个域名解析到多个IP地址?

『SignalR』. Net using signalr for real-time communication

Jmeter--- set proxy recording request

Redis foundation 2 (notes)

2 lines of code to generate a solid desktop background

6-18 vulnerability exploitation - backdoor connection

2022 the latest software tests eight part essay. Whether you can offer depends on how you recite it

Don't vote, software testing posts are saturated
随机推荐
Imitation Tiktok homepage interface
The reisson distributed lock renewal failed due to network reasons, resulting in the automatic release of the lock when the business is still executing but the lock is not renewed.
C语言:随机生成数+冒泡排序
Sofa weekly | open source person - Niu Xuewei, QA this week, contributor this week
【饭谈】那些看似为公司着想,实际却很自私的故事 (一:造轮子)
[hand torn STL] unordered_ set、unordered_ Map (encapsulated with hash table)
dovecot 设置邮箱quota
EL表达式改进JSP
At present, flynk CDC does not support mysql5.5. If you change the source code and release this restriction, there will be a lot of data problems?
聚名十年,说出你的故事,百万豪礼等你拿
Tesseract OCR初探
Dovecot set mailbox quota
Jmeter--- set proxy recording request
突破性思维在测试工作中的应用
Open source RSS subscriber freshrss
[fan Tan] in detail: lower control, upward management, upward drawing cake.
Redis是什么?简述它的优缺点
[go basics 02] the first procedure
LeetCode_ 93_ Restore IP address
Redisv6.0为何引入多线程?