当前位置:网站首页>Swift bit operation exercise
Swift bit operation exercise
2022-07-03 12:38:00 【iWillook】
//
// main.swift
// Ultimate
//
// Created by Mewlan Musajan on 5/7/21.
//
// Excerpt From: Apple Inc. “The Swift Programming Language (Swift 5.3).” Apple Books. https://books.apple.com/us/book/the-swift-programming-language-swift-5-4/id881256329
extension BinaryInteger {
var binaryDescription: String {
var binaryString = ""
var internalNumber = self
for counter in (1...self.bitWidth) {
binaryString.insert(contentsOf: "\(internalNumber & 1)", at: binaryString.startIndex)
internalNumber >>= 1
if counter % 4 == 0 {
binaryString.insert(contentsOf: "_", at:
binaryString.startIndex)
}
}
return "0b" + binaryString
}
}
let pink: UInt32 = 0xCC6699
let redComponent = (pink & 0xFF0000) >> 16
let greenComponent = (pink & 0x00FF00) >> 8
let blueComponent = pink & 0x0000FF
print(pink.binaryDescription)
print(redComponent.binaryDescription)
print(greenComponent.binaryDescription)
print(blueComponent.binaryDescription)
print()
print(pink.binaryDescription)
print("&")
print(Int32(0xFF0000).binaryDescription)
print("===")
print((pink & 0xFF0000).binaryDescription)
边栏推荐
猜你喜欢

The latest version of blind box mall thinkphp+uniapp

Cloud Computing future - native Cloud

剑指Offer06. 从尾到头打印链表

剑指Offer07. 重建二叉树

Integer int compare size

Implement verification code verification

2.8 overview of ViewModel knowledge
![Sword finger offer03 Repeated numbers in the array [simple]](/img/cf/c1ad2f2a45560b674b5b8c11fed244.png)
Sword finger offer03 Repeated numbers in the array [simple]

剑指Offer05. 替换空格

Flutter 退出登录二次确认怎么做才更优雅?
随机推荐
2020-09_ Shell Programming Notes
Recovery of website address and method of Amazon account login two-step verification failure caused by mobile phone number becoming empty
Togaf certification self-study classic v2.0
ImportError: No module named examples. tutorials. mnist
TOGAF认证自学宝典V2.0
Computer version wechat applet full screen display method, mobile phone horizontal screen method.
wpa_ cli
2.6 preliminary cognition of synergetic couroutines
Airflow installation jump pit
242. Effective letter heteronyms
社交社区论坛APP超高颜值UI界面
Swift5.7 扩展 some 到泛型参数
实现验证码验证
The latest version of blind box mall thinkphp+uniapp
Lambda表达式
Wechat applet - basic content
Alibaba is bigger than sending SMS (user microservice - message microservice)
低代码平台国际化多语言(i18n)技术方案
【ManageEngine】IP地址扫描的作用
MySQL time zone solution