TypeScript 运算符
应用场景(待完善)
位运算符
| 运算符 | 描述 | 例子 | 类似于 | 结果 | 十进制 | |
|---|---|---|---|---|---|---|
| & | AND,按位与处理两个长度相同的二进制数,两个相应的二进位都为 1,该位的结果值才为 1,否则为 0。 | x = 5 & 1 | 0101 & 0001 | 0001 | 0001 | 1 |
| | | OR,按位或处理两个长度相同的二进制数,两个相应的二进位中只要有一个为 1,该位的结果值为 1。 | x = 5 | 1 | 0101 | 0001 | 0101 | 5 | |
| ~ | 取反,取反是一元运算符,对一个二进制数的每一位执行逻辑反操作。使数字 1 成为 0,0 成为 1。 | x = ~ 5 | ~0101 | 1010 | -6 | |
| ^ | 异或,按位异或运算,对等长二进制模式按位或二进制数的每一位执行逻辑异按位或操作。操作的结果是如果某位不同则该位为 1,否则该位为 0。 | x = 5 ^ 1 | 0101 ^ 0001 | 0100 | 4 | |
| << | 左移,把 << 左边的运算数的各二进位全部左移若干位,由 << 右边的数指定移动的位数,高位丢弃,低位补 0。 | x = 5 << 1 | 0101 << 1 | 1010 | 10 | |
| >> | 右移,把 >> 左边的运算数的各二进位全部右移若干位,>> 右边的数指定移动的位数。 | x = 5 >> 1 | 0101 >> 1 | 0010 | 2 | |
| >>> | 无符号右移,与有符号右移位类似,除了左边一律使用0 补位。 | x = 2 >>> 1 | 0010 >>> 1 | 0001 | 1 |
实例
var a:number = 2; // 二进制 10
var b:number = 3; // 二进制 11 var result; result = (a & b);
console.log("(a & b) => ",result) result = (a | b);
console.log("(a | b) => ",result) result = (a ^ b);
console.log("(a ^ b) => ",result); result = (~b);
console.log("(~b) => ",result); result = (a << b);
console.log("(a << b) => ",result); result = (a >> b);
console.log("(a >> b) => ",result); result = (a >>> 1);
console.log("(a >>> 1) => ",result);
使用 tsc 命令编译以上代码得到如下 JavaScript 代码
var a = 2; // 二进制 10
var b = 3; // 二进制 11
var result;
result = (a & b);
console.log("(a & b) => ", result);
result = (a | b);
console.log("(a | b) => ", result);
result = (a ^ b);
console.log("(a ^ b) => ", result);
result = (~b);
console.log("(~b) => ", result);
result = (a << b);
console.log("(a << b) => ", result);
result = (a >> b);
console.log("(a >> b) => ", result);
result = (a >>> 1);
console.log("(a >>> 1) => ", result);
TypeScript 运算符的更多相关文章
- TypeScript作为前端开发你必须学习的技能(三)
TypeScript 运算符 TypeScript 主要包含以下几种运算: 算术运算符 逻辑运算符 关系运算符 按位运算符 赋值运算符 三元/条件运算符 字符串运算符 类型运算符 实例看图: 关系运算 ...
- 转载:《TypeScript 中文入门教程》 2、枚举
版权 文章转载自:https://github.com/zhongsp 建议您直接跳转到上面的网址查看最新版本. 由于第一章节是我翻译的,而且与他的版本不一致,导致第一章节有枚举这部分,而他的第二章节 ...
- TypeScript入门-枚举、类型推论
枚举 使用枚举可以定义一些具有名字的数字常量,和在C语言中一样都是使用关键字enum enum Direction { Up = 1, Down = 1<<2, Left, Right } ...
- 迈向angularjs2系列(1):typescript指南
typescript指南 前言 typescript是angularjs2推荐使用的脚本语言.它由微软2012年首次发布. 一. typescript和javascript的区别 1.从遵循的规 ...
- Typescript 基础应用
什么是 TypeScript TypeScript 是微软开发的 JavaScript 的超集,TypeScript兼容JavaScript,可以载入JavaScript代码然后运行.TypeScri ...
- 前端入门25-福音 TypeScript
声明 本篇内容摘抄自以下来源: TypeScript 中文网 只梳理其中部分知识点,更多更详细内容参考官网. 正文-TypeScript 今天来讲讲有 Java 基础转 JavaScript 的福音: ...
- typescript精简版1:用ts表示常见数据类型
一:工程准备: 1.全局安装typescript npm i typescript -g // 或 yarn global add typescript vscode 配置 在命令行执行 tsc -- ...
- ES6 扩展运算符 三个点(...)
它是什么 es6中引入扩展运算符(...),它用于把一个数组转化为用逗号分隔的参数序列,它常用在不定参数个数时的函数调用,数组合并等情形.因为typeScript是es6的超集,所以typeScrip ...
- Typescript 学习笔记三:函数
中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...
随机推荐
- 面试准备之三Django知识
Django请求流程 MTV模式 路由 视图 ORM 模板
- Incorrect string value: '\xE8\xAF\xAD\xE6\x96\x87' for column 'name' at row 1
报错的原因就是在执行插入时对Name这个字段被赋予了错误的字符串值:’\xE4\xB8\xAD\xE6\x96\x87’ 实际上就函数里面的变量接收到的值编码格式跟它定义的不一致. 使用navicat ...
- tpcc-mysql安装测试与使用生成对比图
1:下载tpcc-mysql的压缩包,从下面的网站进行下载 https://github.com/Percona-Lab/tpcc-mysql 也可直接从叶总博客直接下载: http://imysql ...
- SVNKit学习——基于Repository的操作之print repository tree、file content、repository history(四)
此篇文章同样是参考SVNKit在wiki的官方文档做的demo,每个类都可以单独运行.具体的细节都写到注释里了~ 开发背景: SVNKit版本:1.7.14 附上官网下载链接:https://www. ...
- Struts2学习-ssh框架
SSH是 struts+spring+hibernate的一个集成框架,是目前比较流行的一种Web应用程序开源框架. http://www.cnblogs.com/laibin/p/5847111.h ...
- 【深入理解JAVA虚拟机】第二部分.内存自动管理机制.4.JVM工具
1.概述 工具作用:性能监控与故障处理 工作原理:分析数据 数据包含:运行日志. 异常堆栈. GC日志. 线程快照(threaddump/javacore文件). 堆转储快照(heapdump/hpr ...
- June 27th 2017 Week 26th Tuesday
Happiness takes no account of time. 幸福不觉光阴过. At the beginning of this week, I planned to make some s ...
- good pics
- c++由string组成的struct初始化崩溃
struct _UserInfo { string username; string password; string ip; string port; } _UserInfo str={}; 这样就 ...
- 请求是如何传递给StandardEngine的?
将请求的传递过程分解学习. CoyoteAdapter将中持有Connector的引用,所以在Coyote这个类中Connector查找它所属的StandardService,而StandardSer ...