当前位置:网站首页>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)
边栏推荐
- Self made pop-up input box, input text, and click to complete the event.
- Itext7 uses iexternalsignature container for signature and signature verification
- 记录自己vulnhub闯关记录
- 239. Sliding window maximum
- 强大的头像制作神器微信小程序
- Public and private account sending prompt information (user microservice -- message microservice)
- 257. All paths of binary tree
- What is more elegant for flutter to log out and confirm again?
- Applet wxss introduction
- How to deploy web pages to Alibaba cloud
猜你喜欢
剑指Offer09. 用两个栈实现队列
ES6 standard
Day 1 of kotlin learning: simple built-in types of kotlin
【附下载】密码获取工具LaZagne安装及使用
TOGAF认证自学宝典V2.0
Cloud Computing future - native Cloud
Swagger
Alibaba is bigger than sending SMS (user microservice - message microservice)
剑指Offer07. 重建二叉树
Togaf certification self-study classic v2.0
随机推荐
剑指Offer07. 重建二叉树
Do you feel like you've learned something and forgotten it?
Dix règles de travail
Take you to the installation and simple use tutorial of the deveco studio compiler of harmonyos to create and run Hello world?
Approve iPad, which wants to use your icloud account
使用BLoC 构建 Flutter的页面实例
Apache Mina开发手册
Introduction to concurrent programming (II)
Eureka自我保护
Jsup crawls Baidu Encyclopedia
2021 autumn Information Security Experiment 1 (password and hiding technology)
I'm too lazy to write more than one character
剑指Offer03. 数组中重复的数字【简单】
Keep learning swift
1-2 project technology selection and structure
Dart: self study system
Develop plug-ins for idea
CNN MNIST handwriting recognition
Record your vulnhub breakthrough record
【附下载】密码获取工具LaZagne安装及使用