我知道,42是所有事情的答案,但这是怎么回事42
?
int x = -(~'+'|0xAB^1337); //42
答案 0 :(得分:29)
要了解会发生什么,您应该查看此表达式中值的二进制表示。但在此之前,让我们看一下如何将字符转换为整数,以及在这里使用位运算符。
'+'
字符相当于整数值43
- 您可以通过将其转换为整数或使用Convert.ToInt32('+')
来检查它。
0xAB
是hexadecimal literal,代表十进制值171
。
这里你应该理解的最后一件事是bitwise operators:
~
按位NOT - 运算符查看表达式值的二进制表示,并对其执行按位求反运算。表达式中任何1的数字在结果中变为0。表达式中任何0的数字在结果中变为1。|
按位OR - 查看两个表达式的值的二进制表示,并对它们执行按位OR运算。每当任何一个表达式的数字为1时,结果将在该数字中有1。否则,结果将在该数字中为0。^
按位异或 - 查看两个表达式的值的二进制表示,并对它们执行按位异或运算。当一个且只有一个表达式的数字为1时,结果在该数字中为1。否则,结果在该数字中为0。现在所有表达式都看起来像-(NOT(43) OR (171 XOR 1337))
。让我们转到二进制表示这些数字和操作:
Binary | Decimal | Comments
00000000000000000000000000101011 | 43 | '+'
11111111111111111111111111010100 | -44 | NOT 43 // bits are inverted
00000000000000000000000010101011 | 171 | 0xAB
00000000000000000000010100111001 | 1337 |
00000000000000000000010110010010 | 1426 | 171 XOR 1337 // different bits produce 1
11111111111111111111111111010100 | -44 | NOT 43
00000000000000000000010110010010 | 1426 | 171 XOR 1337
11111111111111111111111111010110 | -42 | -44 OR 1426 // 0 only if both bits are 0
00000000000000000000000000101010 | 42 | -(-42)
答案 1 :(得分:15)
~'+'
,它是'+'
=>的ascii值的二进制非值-44 0xAB^1337
(二进制XOR)=> 1426 -44|1426
(二进制OR)=> -42 -42
(算术否定)=> 42 答案 2 :(得分:8)
好吧,让我们一步一步解码:
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title pull-left" *ngIf="empInfo && empInfo.length > selectedEmployee">{{empInfo[selectedEmployee].LastName | uppercase}} Summary</h3>
<div style="float: right;">
<div class="onoffswitch">
<input [(ngModel)]="checkboxValue" (change)="changeTime()" type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" checked>
<label class="onoffswitch-label" for="myonoffswitch">
<span class="onoffswitch-inner"></span>
<span class="onoffswitch-switch"></span>
</label>
</div>
</div>
<div class="clearfix"></div>
</div>
<div class="panel-body">
<form class="form-horizontal" role="form" style="overflow-x:auto;">
<fieldset>
<div class="col-xs-6">
<div class="form-group" *ngIf="empInfo && empInfo.length > selectedEmployee">
<label class="col-xs-5"> Status </label>
<div class="col-xs-7">
<select class="form-control" id="empStatus" [(ngModel)]="empInfo[selectedEmployee].EmpStat" name="empStatus">
<option value="Current">Current</option>
<option value="Terminated">Terminated</option>
</select>
</div>
</div>
</div>
<div class="col-xs-6">
<div class="form-group" *ngIf="empInfo && empInfo.length > selectedEmployee">
<label class="col-xs-5"> Anniversary </label>
<div class="col-xs-7">
<input class='form-control' type="text" id="empAnniversary" [(ngModel)]="empInfo[selectedEmployee].Anniversary" name="empAnniversary"/>
</div>
</div>
</div>
<div class="col-xs-6">
<div class="form-group" *ngIf="empInfo && empInfo.length > selectedEmployee">
<label class="col-xs-5"> Start Date </label>
<div class="col-xs-7">
<input class='form-control' type="text" id="empStartDate" [ngModel]="empInfo[selectedEmployee].StartDate | date: 'MM/dd/y'" name="empStartDate"/>
</div>
</div>
</div>
<div class="col-xs-6">
<div class="form-group" *ngIf="empInfo && empInfo.length > selectedEmployee">
<label class="col-xs-5"> Adjusted Start </label>
<div class="col-xs-7">
<input class='form-control' type="text" id="empAdjustedStart" [ngModel]="empInfo[selectedEmployee].AdjustedStart | date: 'MM/dd/y'" name="empAdjustedStart"/>
</div>
</div>
</div>
<div class="col-xs-6">
<div class="form-group" *ngIf="empInfo && empInfo.length > selectedEmployee">
<label class="col-xs-5"> STD/LTD </label>
<div class="col-xs-7">
<input class='form-control' type="text" id="empSTDLTD" [(ngModel)]="empInfo[selectedEmployee].STDLTD + timeVar" name="empSTDLTD"/>
</div>
</div>
</div>
<div class="col-xs-6">
<div class="form-group" *ngIf="empInfo && empInfo.length > selectedEmployee">
<label class="col-xs-5"> Uncharged </label>
<div class="col-xs-7">
<input class='form-control' type="text" id="empUncharged" [(ngModel)]="empInfo[selectedEmployee].Uncharged + timeVar" name="empUncharged"/>
</div>
</div>
</div>
</fieldset>
<fieldset>
<h4>PTO</h4>
<br />
<div class="col-xs-12">
<div class="form-group" *ngIf="empInfo && empInfo.length > selectedEmployee">
<div class="col-xs-1"></div>
<label class="col-xs-2"> Base </label>
<div class="col-xs-3">
<input class='form-control' type="text" id="ptoBase" [(ngModel)]="empInfo[selectedEmployee].PTOBase + timeVar" name="ptoBase"/>
</div>
<div class="col-xs-6">
</div>
</div>
</div>
<div class="col-xs-6">
<div class="form-group" *ngIf="empInfo && empInfo.length > selectedEmployee">
<label class="col-xs-2" style="font-weight: bold;"> + </label>
<label class="col-xs-4"> Carryover </label>
<div class="col-xs-6">
<input class='form-control' type="text" id="ptoCarry" [(ngModel)]="empInfo[selectedEmployee].PTOCarry + timeVar" name="ptoCarry"/>
</div>
</div>
</div>
<div class="col-xs-6">
<div class="form-group" *ngIf="empInfo && empInfo.length > selectedEmployee">
<div class="col-xs-1"></div>
<label class="col-xs-4"> Balance </label>
<div class="col-xs-6">
<input class='form-control' type="text" id="ptoBalance" [(ngModel)]="empInfo[selectedEmployee].PTOBalance + timeVar" name="ptoBalance"/>
</div>
<div class="col-xs-1"></div>
</div>
</div>
<div class="col-xs-6">
<div class="form-group" *ngIf="empInfo && empInfo.length > selectedEmployee">
<label class="col-xs-2" style="font-weight: bold;"> ― </label>
<label class="col-xs-4"> Borrowed </label>
<div class="col-xs-6">
<input class='form-control' type="text" id="ptoBorrowed" [(ngModel)]="empInfo[selectedEmployee].PTOBorrowed + timeVar" name="ptoBorrowed"/>
</div>
</div>
<hr style="border: solid 1px black;border-bottom:1px solid black;clear:both" />
</div>
<div class="col-xs-6">
<div class="form-group" *ngIf="empInfo && empInfo.length > selectedEmployee">
<label class="col-xs-1" style="font-weight: bold;"> ― </label>
<label class="col-xs-4"> Requests </label>
<div class="col-xs-6">
<input class='form-control' type="text" id="ptoRequests" [(ngModel)]="empInfo[selectedEmployee].PTORequests + timeVar" name="ptoRequests"/>
</div>
<div class="col-xs-1"></div>
</div>
<hr style="border: solid 1px black;border-bottom:1px solid black;clear:both" />
</div>
<div class="col-xs-6">
<div class="form-group" *ngIf="empInfo && empInfo.length > selectedEmployee">
<label class="col-xs-2" style="font-weight: bold;"> = </label>
<label class="col-xs-4"> Balance </label>
<div class="col-xs-6">
<input class='form-control' type="text" id="ptoBalance" [(ngModel)]="empInfo[selectedEmployee].PTOBalance + timeVar" name="ptoBalance"/>
</div>
</div>
</div>
<div class="col-xs-6">
<div class="form-group" *ngIf="empInfo && empInfo.length > selectedEmployee">
<label class="col-xs-1" style="font-weight: bold;"> = </label>
<label class="col-xs-4"> Available </label>
<div class="col-xs-6">
<input class='form-control' type="text" id="ptoRemaining" [(ngModel)]="empInfo[selectedEmployee].PTORemaining + timeVar" name="ptoRemaining"/>
</div>
<div class="col-xs-1"></div>
</div>
</div>
</fieldset>
<fieldset>
<h4>ETO</h4>
<br />
<div class="col-xs-6">
<div class="form-group" *ngIf="empInfo && empInfo.length > selectedEmployee">
<div class="col-xs-2"></div>
<label class="col-xs-4"> Earned </label>
<div class="col-xs-6">
<input class='form-control' type="text" id="etoEarned" [(ngModel)]="empInfo[selectedEmployee].ETOEarned + timeVar" name="etoEarned"/>
</div>
</div>
<div class="form-group" *ngIf="empInfo && empInfo.length > selectedEmployee">
<label class="col-xs-2"> ― </label>
<label class="col-xs-4"> Requests </label>
<div class="col-xs-6">
<input class="form-control" type="text" id="etoRequests" [(ngModel)]="empInfo[selectedEmployee].ETORequests + timeVar" name="etoRequests"/>
</div>
</div>
<hr style="border: solid 1px black;border-bottom:1px solid black;clear:both" />
<div class="form-group" *ngIf="empInfo && empInfo.length > selectedEmployee">
<label class="col-xs-2"> = </label>
<label class="col-xs-4"> Available </label>
<div class="col-xs-6">
<input class='form-control' type="text" id="etoRemaining" [(ngModel)]="empInfo[selectedEmployee].ETORemaining + timeVar" name="etoRemaining"/>
</div>
</div>
</div>
<div class="col-xs-6"></div>
</fieldset>
</form>
</div>
</div>
答案 3 :(得分:3)
转换为十六进制提供了一种相对简单的手动二进制计算方法:
'+'
的代码为0x2B,或二进制的00101011
0x2B
的倒置位模式为0xFFFFFFD4
^
的优先级高于OR |
,因此0xAB
首先与1337进行异或(1337为0x539
)000010101011 ^ 010100111001 = 010110010010
或0x592
0xFFFFFFD4
和0x00000592
的按位OR生成0xFFFFFD6
0x0000002A
,等于小数为42。