- Learning Scala Programming
- Vikash Sharma
- 184字
- 2021-06-30 19:07:52
Bitwise operators
We can perform operations on individual bits of Integer types using Bitwise operators. These includes Bitwise AND (&), OR (|), and XOR (^):
scala> 1 & 2
res2: Int = 0
scala> 1 | 2
res3: Int = 3
scala> 1 ^ 2
res5: Int = 3
These operators can be performed only on Int. If you try this on Double, it'll throw an error: value & is not a member of Double. These operators perform operations on individual bits; in our case, 1 is converted into bits as 01 and 2 as 10, and then AND, OR, and XOR operations are performed:
- 0001 AND 0010 resulted into 00 means 0
- 0001 OR 0010 resulted into 11 means 3
- 0001 XOR 0010 resulted into 11 means 3
We can perform a logical not-operating using ~ operator:
scala> ~2
res8: Int = -3
There are also three shift methods for Int types named shift right (>>), shift left (<<) and unsigned-shift right (>>>). These are binary operators working on two operands. Bits of operand on the left are shifted by value to the right.
推薦閱讀
- JavaScript從入門到精通(微視頻精編版)
- Git Version Control Cookbook
- Learn Type:Driven Development
- 小程序?qū)崙?zhàn)視頻課:微信小程序開發(fā)全案精講
- Python從入門到精通(精粹版)
- OpenGL Data Visualization Cookbook
- App Inventor少兒趣味編程動(dòng)手做
- Ext JS 4 Plugin and Extension Development
- 邊玩邊學(xué)Scratch3.0少兒趣味編程
- Visual Basic語言程序設(shè)計(jì)上機(jī)指導(dǎo)與練習(xí)(第3版)
- Python程序設(shè)計(jì)教程
- 走近SDN/NFV
- Python程序員面試算法寶典
- Learning Unity Physics
- ASP.NET 3.5系統(tǒng)開發(fā)精髓