位运算

栏目: IOS · 发布时间: 6年前

内容简介:位运算了解一下,哈哈哈哈哈哈...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--


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

精通正则表达式

精通正则表达式

弗里德尔 / 东南大学出版社 / 2005-9 / 72.00元

正则表达式是一种用来操作文本和数据的强大工具。近年来,它们快速广泛传播,并被多种流行工具和语言作为标准特性提供,如Perl、Java、VB.NET、C#(及任何使用.NET框架的语言)、PHP、Python、Ruby、Tcl、MySQL、awk、Emacs等。 如果还未使用过正则表达式,从本书中您将发现一个掌控数据的全新世界。如果使用过它们,您将会充分意识到本书空前的深度和广度。如果您认为自己已经......一起来看看 《精通正则表达式》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

URL 编码/解码
URL 编码/解码

URL 编码/解码

MD5 加密
MD5 加密

MD5 加密工具