二进制

func binaryPrintIntNumber(num : Int) {

var remainderArr:[Int] = []  //int数组,存储余数
var quotient:Int = num  //表示商的变量,初始值是num
while quotient > 0 {   //商的值是0
    let remainderNum = quotient % 2 //获取余数的值
    remainderArr.insert(remainderNum, at: 0)    //插入数组
    quotient /= 2   //商变换
}
for item in remainderArr {
    print(item, terminator: "")
}
print("")

}

var a = 10
binaryPrintIntNumber(num: a)
print(a)

你可能感兴趣的:(二进制)