×
D语言教程D语言概述,D语言是什么?D语言开发环境设置D语言基本语法D语言变量D语言数据类型D语言枚举EnumsD语言常值D语言运算符D中算术运算符D语言关系运算符D语言逻辑运算符D语言位运算符D语言赋值运算符D语言sizeof运算符D语言运算符优先级D语言循环D语言while循环D语言for循环D语言do...while循环D语言嵌套循环D语言break语句D语言continue语句D语言决策语句D语言if语句D语言if...else语句D语言if嵌套语句D语言switch语句D语言嵌套switch语句D语言函数D语言字符D语言字符串-StringD语言数组D语言关联数组D语言指针D语言元组D语言结构体D语言联合体D语言范围D语言别名D语言混合类型D语言模块D语言模板D语言常量D语言文件I/OD语言并发D语言异常处理契约式编程D语言条件编译D语言类和对象D语言类成员函数类的访问修饰符构造函数和析构函数this指针类指针类的静态成员类继承重载一元运算符重载二元运算符重载比较操作符重载D语言封装D语言接口D语言抽象类

D语言位运算符


由D语言支持的位运算符列于下表中。假设变量A=60和变量B=13,则:

运算符 描述 示例
& 二进制AND拷贝操作,如果它存在于两个操作数的结果。 (A & B) will give 12 which is 0000 1100
| 二进制OR运算符拷贝位,如果它存在一个操作数中。 (A | B) will give 61 which is 0011 1101
^ 二进位异或运算符拷贝位,如果它被设置在一个操作数,但不能同时使用。 (A ^ B) will give 49 which is 0011 0001
~ 二进制的补码运算符是一元的,具有'翻转'位的效果。 (~A ) will give -61 which is 1100 0011 in 2's complement form due to a signed binary number.
<< 二进制左移位运算符。左操作数的值被移动由右操作数指定的位数。 A << 2 will give 240 which is 1111 0000
>> 二进制右移运算。左操作数的值是正确的由右操作数指定的位数移动。 A >> 2 will give 15 which is 0000 1111

示例

试试下面的例子就明白了所有的D编程语言位运算符:

import std.stdio;

int main(string[] args)
{

   uint a = 60;	/* 60 = 0011 1100 */  
   uint b = 13;	/* 13 = 0000 1101 */
   int c = 0;           

   c = a & b;       /* 12 = 0000 1100 */ 
   writefln("Line 1 - Value of c is %d
", c );

   c = a | b;       /* 61 = 0011 1101 */
   writefln("Line 2 - Value of c is %d
", c );

   c = a ^ b;       /* 49 = 0011 0001 */
   writefln("Line 3 - Value of c is %d
", c );

   c = ~a;          /*-61 = 1100 0011 */
   writefln("Line 4 - Value of c is %d
", c );

   c = a << 2;     /* 240 = 1111 0000 */
   writefln("Line 5 - Value of c is %d
", c );

   c = a >> 2;     /* 15 = 0000 1111 */
   writefln("Line 6 - Value of c is %d
", c );
   return 0;
}

当编译并执行上面的程序它会产生以下结果:

Line 1 - Value of c is 12

Line 2 - Value of c is 61

Line 3 - Value of c is 49

Line 4 - Value of c is -61

Line 5 - Value of c is 240

Line 6 - Value of c is 15

分类导航

关注微信下载离线手册

bootwiki移动版 bootwiki
(群号:472910771)