Java初学者在这里数据类型短

时间:2019-01-01 17:28:35

标签: java

嗨,我只是有一个关于隐式转换的数据类型简短的问题。

这里说short n = 99999;等于-31073的值。为什么等于那个数字?短路的最小数目为-32,768,最大为32,767(含)。另外,我不明白为什么这是负面的?

2 个答案:

答案 0 :(得分:4)

二进制形式的整数值99999是: 1 1000 0110 1001 1111 如您所见,它需要更多的15个二进制数字(+1表示保留+/-符号) short类型的值可以存储的值。 因此,当您在16位空间中“压缩”此值时,您将获得最后16个二进制数字: 1000 0110 1001 1111 等于-31073的短值。

答案 1 :(得分:3)

  1. Java中的ZipArchive::setExternalAttributesName()////// Make Template archive object $templateArchive = new ZipArchive(); $templateArchive->open(PATH_TO_TEMPLATES.'_files/'.$templateName.'/_pack/'.$templateName.'.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE); $files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($templateDir."/templates/".$template_archive_name), RecursiveIteratorIterator::LEAVES_ONLY ); foreach ($files as $name => $file) { // Get real and relative path for current file $filePath = $file->getRealPath(); // relative path is full path, reduced with length of templateDir string and 11 more chars for /templates/ $relativePath = substr($filePath, strlen($templateDir) + 11); // Add regular files if (!$file->isDir()) { // Add current file to archive $templateArchive->addFile($filePath, $relativePath); } elseif (substr($relativePath, -2) === "/.") { // Remove the dot representing the current directory $relativePath = substr($relativePath, 0, -1); // Add current directory to archive $templateArchive->addEmptyDir($relativePath); } else { continue; } $templateArchive->setExternalAttributesName($relativePath, ZipArchive::OPSYS_UNIX, fileperms($filePath) << 16); } // Template Zip archive will be created only after closing object $templateArchive->close(); 等原始类型已签名,但int除外。
  2. Java中的
  3. short是16位带符号整数。

JLS 11 4.2 JLS 8 4.2

然后,对于char整数short,内部二进制文件类似于:

short

当我们在此数字上加1时,我们得到以下二进制:

32767

如果我们将其读为短符号,则为01111111 11111111

然后我们可以获取此映射:

10000000 00000000

对于“短” 99999,可以先将其减少为34463(= 99999%65536),然后由于此映射,应该从65536中减去32768和65535之间的任何数字,然后将34463映射到最终结果中-32768