当前位置:网站首页>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)
边栏推荐
- Swagger
- 十條職場規則
- Dix règles de travail
- initial、inherit、unset、revert和all的区别
- [ManageEngine] the role of IP address scanning
- Kung Fu pays off, and learning is done
- Record your vulnhub breakthrough record
- Kubectl_ Command experience set
- Recovery of website address and method of Amazon account login two-step verification failure caused by mobile phone number becoming empty
- I'm too lazy to write more than one character
猜你喜欢

With pictures and texts, summarize the basic review of C language in detail, so that all kinds of knowledge points are clear at a glance?

If you can't learn, you have to learn. Jetpack compose writes an im app (II)

低代码平台国际化多语言(i18n)技术方案

社交社区论坛APP超高颜值UI界面

LeetCode 0556.下一个更大元素 III - 4步讲完

initial、inherit、unset、revert和all的区别

Prompt unread messages and quantity before opening chat group

【ManageEngine】IP地址扫描的作用

Togaf certification self-study classic v2.0

Record your vulnhub breakthrough record
随机推荐
Adult adult adult
Alibaba is bigger than sending SMS (user microservice - message microservice)
Lambda表达式
Write a simple nodejs script
242. Effective letter heteronyms
Develop plug-ins for idea
Apache Mina开发手册
Wechat applet - basic content
Use of atomicinteger
初入职场,如何快速脱颖而出?
The difference between lambda and anonymous inner class
Public and private account sending prompt information (user microservice -- message microservice)
MySQL time zone solution
error: expected reference but got (raw string)
剑指Offer04. 二维数组中的查找【中等】
启用MemCached的SASL认证
Togaf certification self-study classic v2.0
Fluent: Engine Architecture
十条职场规则
Flutter 退出登录二次确认怎么做才更优雅?