位运算
位运算
Delphi 的按位运算符共有六个: not and or xor shr shl;
其中的 not and or xor 也叫逻辑运算符, 其实功能都是一样的, 因为不管什么数据追到底都是 0 和 1 的组合
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class (TForm) Button1: TButton; Button2: TButton; Button3: TButton; Button4: TButton; Button5: TButton; Button6: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); procedure Button5Click(Sender: TObject); procedure Button6Click(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} const w1: Word = 61680; {二进制表示: 11110000 11110000} w2: Word = 3855; {二进制表示: 00001111 00001111} var w: Word; {not 运算, 只有一个运算数} procedure TForm1.Button1Click(Sender: TObject); begin w := not w1; {not 就是按位(给二进制的每一位)取反} {11110000 11110000 取反后就是:} {00001111 00001111 } ShowMessage(IntToStr(w)); {3855} end; {and 运算, 需要两个运算数} procedure TForm1.Button2Click(Sender: TObject); begin w := w1 and w2; {and 就是把两个运算数按位对比, 如果相同(都是1或都是0)返回1; 不同返回0} {w1: 11110000 11110000 与} {w2: 00001111 00001111 每一位都不同, 所以返回:} {w : 00000000 00000000} ShowMessage(IntToStr(w)); {0} end; {or 运算, 需要两个运算数} procedure TForm1.Button3Click(Sender: TObject); begin w := w1 or w2; {or 就是把两个运算数按位对比, 只有其中一个是1就返回1; 都是0才返回0} {w1: 11110000 11110000 与} {w2: 00001111 00001111 or 后会返回:} {w : 11111111 11111111} ShowMessage(IntToStr(w)); {65535} end; {xor 运算, 需要两个运算数} procedure TForm1.Button4Click(Sender: TObject); begin w := w1 xor w2; {xor 就是把两个运算数按位对比, 只有两个不一样才返回1; 一样(都是0或都是1)则返回0} {w1: 11110000 11110000 与} {w2: 00001111 00001111 xor 后会返回:} {w : 11111111 11111111} ShowMessage(IntToStr(w)); end; {shr 运算, 只有一个运算数} procedure TForm1.Button5Click(Sender: TObject); begin w := w1 shr 1; {shr 是按位右移, shr 1 是右移一位} {w1: 11110000 11110000 右移一位后是:} {w : *1111000 01111000 前面的*就是0了} ShowMessage(IntToStr(w)); {30840} {同理, 可以移动几位, 譬如 3 位} w := w1 shr 3; ShowMessage(IntToStr(w)); {7710} {w1 shr 3 相当与 w1 div 2的3次方} w := w1 div 8; ShowMessage(IntToStr(w)); {7710} end; {shl 运算, 只有一个运算数} procedure TForm1.Button6Click(Sender: TObject); var i: Integer; begin w := w1 shl 1; {shr 是按位左移} {w1: 11110000 11110000 左移一位后是:} {w : 1110000 111100000 } ShowMessage(IntToStr(w)); {57824} {左移 3 位} w := w1 shl 3; ShowMessage(IntToStr(w)); {34688} {w1 shl 3 相当与 w1 * 2的3次方} w := w1 * 8; ShowMessage(IntToStr(w)); {34688} {注意这里有个问题: w1*8 以后怎么小了呢?} {因为前面已经定义了 w 是 Word 类型的, 它的大小只有2个字节(二进制16位), 超出会忽略} {如果换成32位(4字节)的 Integer 类型, 肯定就会有真实的结果:} i := w1 shl 3; ShowMessage(IntToStr(i)); {493440} i := w1 * 8; ShowMessage(IntToStr(i)); {493440} end; end. |
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/11145057.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?