我是Java新手。所以,我想了解背后的工作。
int one_int = 1; byte one = 1; one <<= 7; one_int <<= 7; System.out.println(one_int); //Output is 128 System.out.println(one); //Output is -128
为什么int上左移的输出是128而字节是-128?
答案 0 :(得分:3)
在java中
当您将字节向左移动7次时:
00000001 - &gt;千万
当你向前移动int 7次时:
00000000000000000000000000000001 - &gt; 00000000000000000000000010000000
因为它们都用于呈现已签名和无符号的号码,所以我们使用Two's complement操作。
这意味着第一位(MSB)代表数字的符号(0代表+,1代表 - )。
答案 1 :(得分:0)
两个移位操作都产生相同的二进制值10000000 2 。区别在于对该值最重要的位的解释。
有符号整数的最高位确定数字是正数(零)还是负数(1)。
<form id="profile_form" role="form" action="" method="post" class="form-inline">
<fieldset data-step="0">
<h4>Introduction <span class="step">(Step 1 / 7)</span></h4>
<div class="form-group">
<label for="first-name">First Name:</label><br>
<input type="text" name="firstname" class="first-name form-control" id="first-name">
</div>
<div class="form-group">
<label for="last-name">Last Name:</label><br>
<input type="text" name="last-name" class="last-name form-control" id="last-name">
</div>
<div class="form-group">
<label for="height">Height:</label><br>
<input type="text" name="height" class="height form-control" id="height">
</div>
<div class="form-group">
<label for="weight">Weight:</label><br>
<input type="text" name="weight" class="weight form-control" id="weight">
</div>
<br>
<button type="button" class="btn btn-next">Next <i class="fa fa-angle-right"></i></button>
</fieldset>
<fieldset data-step="1">
<h4>Place and Date of Birth <span class="step">(Step 2 / 7)</span></h4>
<div class="form-group">
<label for="birth-city">City:</label><br>
<input type="text" name="birth-city" class="birth-city form-control" id="birth-city">
</div>
<div class="form-group">
<label for="birth-state">State / Province:</label><br>
<input type="text" name="birth-state" class="birth-state form-control" id="birth-state">
</div>
<div class="form-group">
<label for="birth-country">Country:</label><br>
<input type="text" name="birth-country" class="birth-country form-control" id="birth-country">
</div>
<div class="form-group">
<label for="birth-date">Date (YYYY/MM/DD):</label><br>
<input type="text" name="birth-date" class="birth-date form-control" id="birth-date">
</div>
<br>
<button type="button" class="btn btn-previous"><i class="fa fa-angle-left"></i> Previous</button>
<button type="button" class="btn btn-next">Next <i class="fa fa-angle-right"></i></button>
</fieldset>
</form
是32位类型,因此其最高有效位为零,因此10000000 2 被解释为正128