当前位置:网站首页>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)
边栏推荐
- adb push apk
- How to deploy web pages to Alibaba cloud
- Comprehensive evaluation of double chain notes · Siyuan notes: advantages, disadvantages and evaluation
- 023 ([template] minimum spanning tree) (minimum spanning tree)
- Pytext training times error: typeerror:__ init__ () got an unexpected keyword argument 'serialized_ options'
- Implement verification code verification
- 【嵌入式】---- 内存四区介绍
- 2021 autumn Information Security Experiment 1 (password and hiding technology)
- 剑指Offer10- I. 斐波那契数列
- 【ManageEngine】IP地址扫描的作用
猜你喜欢
随机推荐
Integer int compare size
Day 1 of kotlin learning: simple built-in types of kotlin
TOGAF认证自学宝典V2.0
剑指Offer06. 从尾到头打印链表
Swift Error Handling
Flutter Widget : KeyedSubtree
(最新版) Wifi分销多开版+安装框架
Flutter: self study system
225. Implement stack with queue
Fluent: Engine Architecture
MySQL time zone solution
Introduction to concurrent programming (II)
ES6 standard
公纵号发送提示信息(用户微服务--消息微服务)
使用BLoC 构建 Flutter的页面实例
If you can't learn, you have to learn. Jetpack compose writes an im app (I)
Sword finger offer05 Replace spaces
The difference between lambda and anonymous inner class
adb push apk
The future of cloud computing cloud native