多播期间的行为改变 [英] behavioral change during multicasting

查看:84
本文介绍了多播期间的行为改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是一个简单的陈述

System.out.println((int)(char)(byte)-1);




  1. 当我运行它时输出 65535 。我不知道
    的原因吧。

  2. 执行期间(字节)-1返回-1,因此(int)(char)(字节) -1 相当于(int)(char)-1 。当我打印(字符)-1时,它在某些系统中打印 ,但不在所有系统中打印。

  3. 如果我忽略上面提到的第二点并打印(int)'?'那么它
    打印63

  1. when I run it I get output as 65535. I dont know the reason for it.
  2. During execution (byte)-1 return -1,hence (int)(char)(byte)-1 is equivalent to (int)(char)-1. when I print (char)-1 it prints ? only in some system but not in all systems.
  3. if I ignore the 2nd point above mentioned and print (int)'?' then it prints 63

所以我的问题是,如果我一起进行多播,那是(int)(char)(byte)-1 然后我得到 65535 但是,如果我按部分进行铸造

So my question is if I do multicast all together that is (int)(char)(byte)-1 then I get 65535 but if I do casting part by part that


  • (字节)-1

  • ( char)-1

  • (int)'?'

然后我得到63,为什么是这样吗?

then I get 63,why is it so?

推荐答案

1):

首先,你有一个整数(32位) -1 的二进制表示是:

First, you have an integer (32 bits) the binary representation of -1 is:

1111 1111 1111 1111 1111 1111 1111 1111 

然后,它被转换为一个字节(8位):

then, it is converted to a byte (8 bits):

1111 1111

如果你打印出来,你会得到-1(那是字节代表-1)然后,它被强制转换为 char (一个16位无符号字符),它将为您提供:

If you print that, you'll get -1 (that's the byte representation for -1) then, it is cast to a char (a 16 bit unsigned character) which will give you:

1111 1111 1111 1111

(因为它会扩展标志如果你试图将它打印成一个字符,你将得到一个无法识别的字符(在某些系统中它会打印问号)然后,你做一个转换为 int ;但是, char 是无符号的,所以有符号扩展(因为没有符号),所以你最终得到:

(because it will "expand" the sign) if you try to print this as a char, you'll get an "irrecognizable" character (in some systems it will print the question mark) then, you do a cast to int; but, a char is unsigned, so there is expansion os the sign (because there is no sign), so you finally have:

0000 0000 0000 0000 1111 1111 1111 1111

其中,十进制等于65535

which, in decimal equals 65535

对于2):

否,( int)(char)(byte)-1 不等于(int)(char)-1 (参见#1的解释)

No, (int)(char)(byte)-1 is not equivalent to (int)(char)-1 (see explanation for #1)

这篇关于多播期间的行为改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆