0

I'm new to Java. I saw the code below from Java tutorial Oracle. I'm struggling to understand the purpose of this code snippet:

    public synchronized int getRGB() {
        return ((red << 16) | (green << 8) | blue);
    }

I understand how bitwise operator works but I'm struggling to understand why the getRGB() method is written the way it did. Is there an alternative way to write getRGB() method?

public class SynchronizedRGB {

    // Values must be between 0 and 255.
    private int red;
    private int green;
    private int blue;
    private String name;

    private void check(int red,
            int green,
            int blue) {
        if (red < 0 || red > 255
                || green < 0 || green > 255
                || blue < 0 || blue > 255) {
            throw new IllegalArgumentException();
        }
    }

    public SynchronizedRGB(int red,
            int green,
            int blue,
            String name) {
        check(red, green, blue);
        this.red = red;
        this.green = green;
        this.blue = blue;
        this.name = name;
    }

    public void set(int red,
            int green,
            int blue,
            String name) {
        check(red, green, blue);
        synchronized (this) {
            this.red = red;
            this.green = green;
            this.blue = blue;
            this.name = name;
        }
    }

    public synchronized int getRGB() {
        return ((red << 16) | (green << 8) | blue);
    }

    public synchronized String getName() {
        return name;
    }

    public synchronized void invert() {
        red = 255 - red;
        green = 255 - green;
        blue = 255 - blue;
        name = "Inverse of " + name;
    }

    public static void main(String[] args) {
        SynchronizedRGB color = new SynchronizedRGB(0, 0, 0, "Pitch black");
        System.out.println(color.getRGB());
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Thor
  • 9,638
  • 15
  • 62
  • 137
  • 1
    8 bits is all you need to represent 0 through 255. I suspect it was written this way to prevent excessive use of memory(each colour using a full 32 bit int when they only need 8) and/or clock cycles(three function calls vs one). You can certainly override the method if you wish. – Taelsin Feb 19 '16 at 00:10
  • 1
    It's a packed `int`, this is typically how RGB(A) values are represented in most graphics circles. You could write it to return an `int` array, but what does each element actually mean? You could make a `RGB` class which just contains getters for each value, but it's not as transportable as a packed `int` is – MadProgrammer Feb 19 '16 at 00:10

2 Answers2

2

The left shift operator << x is equivalent to multiplying by 2^x, so you can write it as

return ((red * 65536 ) + (green *256) + blue);
Radu Ionescu
  • 3,462
  • 5
  • 24
  • 43
1

Each component is a byte (8 bits), an integer is 32 bits, so you can store 4 8 bit numbers in a 32 bit integer, 8 bits for alpha, red, green and blue. The >> or << operators are bit shift operators, moving the bits in a number by the specified amount. 1 << 1 results in 2. Looking at the binary sequence... 00000001 << 1 = 00000010