内容简介:位运算了解一下,哈哈哈哈哈哈...Person.swiftmain.swift
位运算了解一下,哈哈哈哈哈哈...
Person.swift
import Foundation
extension Bool {
init(_ integer: Int){
self.init(integer != 0)
}
}
struct Person {
let TallMask = (0b00000001 << 0b00000000)
let RichMask = (0b00000001 << 0b00000001)
let HandsomeMask = (0b00000001 << 0b00000010)
var _tallRichHandsome = 0b00000000
mutating func setTall(_ tall: Bool) {
if tall {
_tallRichHandsome = (_tallRichHandsome | TallMask)
} else {
_tallRichHandsome = (_tallRichHandsome & ~TallMask)
}
}
func isTall() -> Bool {
return Bool(_tallRichHandsome & TallMask)
}
mutating func setRich(_ rich: Bool) {
if rich {
_tallRichHandsome = (_tallRichHandsome | RichMask)
} else {
_tallRichHandsome = (_tallRichHandsome & ~RichMask)
}
}
func isRich() -> Bool {
return Bool(_tallRichHandsome & RichMask)
}
mutating func setHandsome(_ handsome: Bool) {
if handsome {
_tallRichHandsome = (_tallRichHandsome | HandsomeMask)
} else {
_tallRichHandsome = (_tallRichHandsome & ~HandsomeMask)
}
}
func isHandsome() -> Bool {
return Bool(_tallRichHandsome & HandsomeMask)
}
}
main.swift
import Foundation
var person = Person()
print(person.isTall())
print(person.isRich())
print(person.isHandsome())
print("================")
print(person._tallRichHandsome)
print("===================")
person.setTall(true)
person.setRich(true)
person.setHandsome(true)
print(person.isTall())
print(person.isRich())
print(person.isHandsome())
print("===================")
//print(person.TallMask)
//print(person.RichMask)
//print(person.HandsomeMask)
person.setTall(false)
print(person.isTall())
print(person.isRich())
print(person.isHandsome())
print("==================")
//print(0b00000000)
//print(0b00000001)
//print(0b00000010)
print("==================")
print(person._tallRichHandsome)
--EOF--
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 【Java】使用位运算(&)代替取模运算(%)
- ES6—扩展运算符和rest运算符(6)
- JavaScript运算出现很多小数导致运算不精确的问题,用toFixed解决
- C/C++三元运算符实际上是否具有与赋值运算符相同的优先级?
- Python 运算符
- 说说Java 位运算
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
世界是平的(3.0版)
[美] 托马斯·弗里德曼 / 何帆、肖莹莹、郝正非 / 湖南科学技术出版社 / 2008-9 / 58.00元
世界变得平坦,是不是迫使我们跑得更快才能拥有一席之地? 在《世界是平的》中,托马斯·弗里德曼描述了当代世界发生的重大变化。科技和通信领域如闪电般迅速的进步,使全世界的人们可以空前地彼此接近——在印度和中国创造爆炸式增长的财富;挑战我们中的一些人,比他们更快占领地盘。3.0版新增两章,更新了报告和注释方面的内容,这些内容均采自作者考察世界各地特别是整个美国中心地带的见闻,在美国本土,世界的平坦......一起来看看 《世界是平的(3.0版)》 这本书的介绍吧!