objective-C和C --- 《位运算》的使用之二进制颜色的转换(二)

为UIColor写个分类方法

+ (UIColor *)colorWithBinaryHex:(u_int32_t)hex{
    //0x(hex)0000
    int red = (0xFF0000 & hex) >> 16;     //返回还是hex,在FF的位置上,从左数第5位,也就是2的4次方
    int green = (0x00FF00 & hex) >> 8; //返回还是hex,在FF的位置上
    // hex => 0x 0000AA   => 00001010 1010  //返回还是hex,在FF的位置上,个位上
    // 与上 => 0x 0000FF   => 00001111 1111  => 结果还是:000010101010 =>0x0000AA
    int blue = (0x0000FF & hex);
    return [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1.0];
}

 //调用它

self.view.backgroundColor = [UIColor colorWithBinaryHex:0xFF0000];
//返回结果是红色
将来的自己,会感谢现在不放弃的自己!
原文地址:https://www.cnblogs.com/TheYouth/p/6550994.html