热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

PHP教程之变量互换【PHP】

后端开发|php教程变量,教程,the,of,and,that,is,to,后端开发-php教程AttributedtoSolomonW.Golomb;amethodforswap

后端开发|php教程PHP教程之变量互换【PHP】
变量,教程,the,of,and,that,is,to,
后端开发-php教程
Attributed to Solomon W. Golomb; a method for swapping the values of two integer variables without using an intermediate variable (you can tell this dates from the Elder Days, when variables were expensive!). Thanks to PHP’s syntax it’s also a one-liner.
代购网站源码下载,vscode编译运行不弹出黑框,ubuntu给文件改名,tomcat配置文件字段,PHP sqlite 缓存,css中文网页设计模板下载,中国版租赁服务器,dede去格式插件,前端掌握几种框架,大咖爱爬虫,notify.php,做网站seo优化,计算机答辩springboot,phpcmsv9网站建设入门教程,网页烟花,metinfo 模板安装,交互式后台管理模板,动态欢迎页面3d,kingcms 内容管理系统,程序小说lzw
$a^=$b^=$a^=$b;
淘宝号担保交易源码,vscode输入框在哪,安装ubuntu打不开,tomcat6 漏洞,c sqlite 升序,西宁网页设计培训学校,dedecms数据库备份文件,flash 与服务器通信,找钱插件,前端框架electron,爬虫dz论坛,php购物网站,山西seo教程,springboot部署码,动易政府网站源代码,vb 网页标题,layui 后端模板 源码,网页后台怎么做,jquery 输出到页面,帝国网站管理系统 栏目,jsp程序源码lzw
Okay, here’s how it goes (yeah, like I need to make content-free posts just for the sake of an increment…PHP教程之变量互换【PHP】).
超级卡盟源码,vscode调试js和c,ubuntu配置cgi,tomcat中文乱码影响,爬虫数据详解,centos7部署php,潍坊百度seo优化厂家,360类似网站源码,aspcms模板怎么用lzw
First, simplify the line; noting that ^= is right-associative, which means that in that line the rightmost operator is evaluated first, that the assignment operators also return the value that they assign to their lvalue, and that foo^=bar is shorthand for foo=foo^bar:

Code:


$a^=$b^=$a^=$b;

$a^=($b^=($a^=$b));

$a=$a^b;
$a^=($b^=$a);

$a=$a^$b;
$b=$b^$a;
$a=$a^$b;

Recall what ^ does. Takes each pair of corresponding bits from its arguments (the internal binary representation of its arguments, that is), and xors them together to produce the corresponding bit of the result (“corresponding” means that the first bits of both arguments produce the first bit of the result, the second bits of both arguments produce the second bit of the result, and so on. This is why, as BuzzLY noted, it’s important that both variables are the same size – demons probably start flying out of your nose if one of them runs out of bits to xor before the other. So to figure out what ^ does to a pair of variables, we only need to recap what it does to single bits


Code:


x y | x^y
——+—-
0 0 | 0
0 1 | 1
1 0 | 1
1 1 | 0
Basically, x^y is true if x and y are different, and is false otherwise. In other words, x is true or y is true, but they’re not both true. Hence, “exclusive-or”.

Now, taking those three lines, we see what happens when $a and $b start out with initial values $s and $t.


Code:


$a = $s;
$b = $t;
$a=$a^$b;
$b=$b^$a;
$a=$a^$b;

// Since $b==$t, we substitute $t for $b for as long as $b doesn’t change
$a = $s;
$a=$a^$t;
$b=$t^$a;
$a=$a^$b;

// And likewise for $a
$a=$s^$t;
$b=$t^$a;
$a=$a^$b;

// But don’t stop there!
$b=$t^($s^$t);
$a=($s^$t)^$b;

$b=$t^($s^$t);
$a=($s^$t)^($t^($s^$t));

From the table above, it’s obvious that $a^$b=$b^$a (if $a is different from $b, then $b must be different from $a), and that $a^$a=0 ($a is not different from itself). Using those two facts, that the fact that $a^0 = $a and $a^($b^$c) = ($a^$b)^$c (which I leave as exercises for the reader), we can simplify those two rather long expressions.


Code:


$b=$t^($s^$t);
$a=($s^$t)^($t^($s^$t));

$b=$t^($t^$s);
$a=($t^$s)^($t^($t^$s));

$b=($t^$t)^$s;
$a=($t^$s)^(($t^$t)^$s);

$b=0^$s;
$a=($t^$s)^(0^$s);

$b=$s;
$a=($t^$s)^$s;

$b=$s;
$a=$t^($s^$s);

$b=$s;
$a=$t^0;

$b=$s;
$a=$t;


So after starting out with $a=$s and $b=$t, we end up with $a=$t and $b=$s. In other words, ^ swapped the each pair of bits of $a and $b with each other. Do that for all the bits and the result is one of $a and $b being swapped.


And after all that is it any mystery why (a) XOR is such a useful operation in cryptography, and (b) learning a bit of maths (boolean algebra in this case) can be useful in programming?


推荐阅读
  • 本文介绍了通过ABAP开发往外网发邮件的需求,并提供了配置和代码整理的资料。其中包括了配置SAP邮件服务器的步骤和ABAP写发送邮件代码的过程。通过RZ10配置参数和icm/server_port_1的设定,可以实现向Sap User和外部邮件发送邮件的功能。希望对需要的开发人员有帮助。摘要长度:184字。 ... [详细]
  • 本文内容为asp.net微信公众平台开发的目录汇总,包括数据库设计、多层架构框架搭建和入口实现、微信消息封装及反射赋值、关注事件、用户记录、回复文本消息、图文消息、服务搭建(接入)、自定义菜单等。同时提供了示例代码和相关的后台管理功能。内容涵盖了多个方面,适合综合运用。 ... [详细]
  • 如何实现织梦DedeCms全站伪静态
    本文介绍了如何通过修改织梦DedeCms源代码来实现全站伪静态,以提高管理和SEO效果。全站伪静态可以避免重复URL的问题,同时通过使用mod_rewrite伪静态模块和.htaccess正则表达式,可以更好地适应搜索引擎的需求。文章还提到了一些相关的技术和工具,如Ubuntu、qt编程、tomcat端口、爬虫、php request根目录等。 ... [详细]
  • 本文介绍了使用AJAX的POST请求实现数据修改功能的方法。通过ajax-post技术,可以实现在输入某个id后,通过ajax技术调用post.jsp修改具有该id记录的姓名的值。文章还提到了AJAX的概念和作用,以及使用async参数和open()方法的注意事项。同时强调了不推荐使用async=false的情况,并解释了JavaScript等待服务器响应的机制。 ... [详细]
  • 本文介绍了如何使用php限制数据库插入的条数并显示每次插入数据库之间的数据数目,以及避免重复提交的方法。同时还介绍了如何限制某一个数据库用户的并发连接数,以及设置数据库的连接数和连接超时时间的方法。最后提供了一些关于浏览器在线用户数和数据库连接数量比例的参考值。 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • 本文详细介绍了Linux中进程控制块PCBtask_struct结构体的结构和作用,包括进程状态、进程号、待处理信号、进程地址空间、调度标志、锁深度、基本时间片、调度策略以及内存管理信息等方面的内容。阅读本文可以更加深入地了解Linux进程管理的原理和机制。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • 本文介绍了高校天文共享平台的开发过程中的思考和规划。该平台旨在为高校学生提供天象预报、科普知识、观测活动、图片分享等功能。文章分析了项目的技术栈选择、网站前端布局、业务流程、数据库结构等方面,并总结了项目存在的问题,如前后端未分离、代码混乱等。作者表示希望通过记录和规划,能够理清思路,进一步完善该平台。 ... [详细]
  • 后台获取视图对应的字符串
    1.帮助类后台获取视图对应的字符串publicclassViewHelper{将View输出为字符串(注:不会执行对应的ac ... [详细]
  • 本文介绍了Web学习历程记录中关于Tomcat的基本概念和配置。首先解释了Web静态Web资源和动态Web资源的概念,以及C/S架构和B/S架构的区别。然后介绍了常见的Web服务器,包括Weblogic、WebSphere和Tomcat。接着详细讲解了Tomcat的虚拟主机、web应用和虚拟路径映射的概念和配置过程。最后简要介绍了http协议的作用。本文内容详实,适合初学者了解Tomcat的基础知识。 ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • 本文介绍了关于apache、phpmyadmin、mysql、php、emacs、path等知识点,以及如何搭建php环境。文章提供了详细的安装步骤和所需软件列表,希望能帮助读者解决与LAMP相关的技术问题。 ... [详细]
author-avatar
啊哈哈
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有