先看代码:
$a=8; $b=~$a; //按位取反 echo "\$a=".$a."<br>"; echo "\$b=".$b;
输出结果:
$a=8 $b=-9
结果跟我所想的不一样啊,因为在PHP中文手册中说: ~ $a Not(按位非) 将 $a 中为 0 的位设为 1,反之亦然。 我只是简单的理解为:0变1,1变0。原来按位取反以十进制输出并不是简单理解成这样。
首先要知道,在计算机中,负数以其正值的补码形式表达。
这样我们就要知道三个概念了,原码,反码和补码。
原码:一个整数,按照绝对值大小转换成的二进制数,称为原码。
反码:将二进制数按位取反,所得的新二进制数称为原二进制数的反码。取反操作指:1变0, 0变1。
补码:反码加1称为补码。也就是说,要得到一个数的补码,先得到反码,然后将反码加上1,所得数称为补码。
正数的补码与其原码相同;负数的补码是在其反码的末位加1
由上所知,8按位取反以十进制输出的过程:
1、8的原码为:0000 0000 0000 0000 0000 0000 0000 1000
2、按位取反:1111 1111 1111 1111 1111 1111 1111 0111;由于最前面一位为1,所以为负数。即即按位取反后得的为负数
下面以十进制输出:负数以其正值的补码形式表达:
3、2步所得的数(是其正为取反,符号不变,最高位还是为1)取反:1000 0000 0000 0000 0000 0000 000 1000
4、反码加1:1000 0000 0000 0000 0000 0000 000 1001;这样就得负数的正值的补码形式。也就是-9了。
再举一例子啊。如2按位取反。
0000 0000 0000 0000 0000 0000 0000 0010(原码)
1111 1111 1111 1111 1111 1111 1111 1101(按位取反)
1000 0000 0000 0000 0000 0000 000 0010 (正值的反码,符号不变)
1000 0000 0000 0000 0000 0000 000 0011 (加1,结果为-3)
代码和结果如下:
<?php $a=2; $b=~$a; //按位取反 echo "\$a=".$a."<br>"; echo "\$b=".$b; ?>
结果:
$a=2 $b=-3
The above is the detailed content of What does bitwise negation mean in PHP bitwise operations?. For more information, please follow other related articles on the PHP Chinese website!

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver Mac version
Visual web development tools
