位移位与位操作,以EN code RGB值 [英] Bit Shift and Bitwise operations to encode RGB values

查看:142
本文介绍了位移位与位操作,以EN code RGB值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想带code中的RGB颜色以一个整数值。

假设算法进行编码是这样的:

  INT code =(蓝色* 256 * 256)+(绿色* 256)+红

如何能连接code /德code RGB分量/从code。使用移位和/或逐位运算符?


解决方案

  INT blueMask =为0xFF0000,greenMask =为0xFF00,redMask = 0xFF的;
    INT R = 12,G = 13,B = 14;
    INT bgrValue =(B<< 16)+(G<< 8)+ R;
    的System.out.println(蓝:+((bgrValue&安培; blueMask)GT;> 16));
    的System.out.println(红+((bgrValue&安培; redMask)));
    的System.out.println(绿色+((bgrValue&安培; greenMask)GT;> 8));

I would like to encode the RGB color to a single integer value.

Let's say the algorithm for encoding is like this:

int code = (blue * 256 * 256) + (green * 256) + red

How can encode/decode RGB components to/from the code using bit shift and/or bitwise operators?

解决方案

    int blueMask = 0xFF0000, greenMask = 0xFF00, redMask = 0xFF;
    int r = 12, g = 13, b = 14;
    int bgrValue = (b << 16) + (g << 8) + r;
    System.out.println("blue:" + ((bgrValue & blueMask) >> 16));
    System.out.println("red:" + ((bgrValue & redMask)));
    System.out.println("green:" + ((bgrValue & greenMask) >> 8));

这篇关于位移位与位操作,以EN code RGB值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆