0

This code works perfectly for what I am trying to do (set the color/alpha of a pixel on a bufferedimage) but I have no idea why it works. What does the syntax mean? I couldn't find documentation on it somehow.

int rgb = (alpha << 24) | red << 16) | (green << 8) | blue;

Also what form is the result? (What would it be called) Lastly, is there a way to convert the img.getRGB() into sepperate a, r, g, b values?

Miles
  • 487
  • 3
  • 12
  • 1
    The short answer: `<<` indicates a bit shift. http://stackoverflow.com/questions/141525/absolute-beginners-guide-to-bit-shifting can provide more information about it – Chris Forrence Mar 31 '15 at 17:04
  • Strongly related: [How to pack ARGB to one integer uniquely?](http://stackoverflow.com/questions/7358533/how-to-pack-argb-to-one-integer-uniquely). – rgettman Mar 31 '15 at 17:06

3 Answers3

3

This syntax combines bits of four eight-bit integer numbers ranging from 0 to 255 into a single 32-bit integer.

The << operator shifts each 8-bit number into its final position in the 32-bit number, adding zeros to the "tail" as needed; the | operator then combines the four parts into a single number.

aaaaaaaa 00000000 00000000 00000000 // aaaaaaaa << 24
00000000 rrrrrrrr 00000000 00000000 // rrrrrrrr << 16
00000000 00000000 gggggggg 00000000 // gggggggg << 8
00000000 00000000 00000000 bbbbbbbb // bbbbbbbb
-----------------------------------
aaaaaaaa rrrrrrrr gggggggg bbbbbbbb

Each letter above corresponds to individual bits of each of the four components:

  • aaaaaaaa are the eight bits of alpha
  • rrrrrrrr are the eight bits of red
  • gggggggg are the eight bits of green
  • bbbbbbbb are the eight bits of blue
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

The << is the left shift operator: It moves the bits in the left-hand operand to the left by the number of places in the right-hand operand.

The | is a bitwise OR operator: It does an OR operation on each bit of the operands, returning a result where each bit is 1 if either operand had a 1 there, or 0 if neither did.

So it:

  1. Takes the alpha value and shifts it 24 bits to the left
  2. Takes the red value and shifts it 16 bits to the left
  3. Takes the green value and shifts it 8 bits to the left
  4. ORs the results together

So say you start out with

alpha = 106 (in binary: 00000000 00000000 00000000 01101010)
red =   255 (in binary: 00000000 00000000 00000000 11111111)
green =  15 (in binary: 00000000 00000000 00000000 00001111)
blue =  170 (in binary: 00000000 00000000 00000000 10101010)

So first it shifts the value of alpha 24 bits to the left:

alpha = 00000000 00000000 00000000 01101010
<< 24 becomes:
alpha = 01101010 00000000 00000000 00000000

Then red 16 bits left:

red =   00000000 00000000 00000000 11111111
<< 16 becomes:
red =   00000000 11111111 00000000 00000000

Then green 8 bits left:

green = 00000000 00000000 00000000 00001111
<< 9 becomes:
green = 00000000 00000000 00001111 00000000

Then it ORs them all together with blue:

alpha = 01101010 00000000 00000000 00000000
red =   00000000 11111111 00000000 00000000
green = 00000000 00000000 00001111 00000000
blue =  00000000 00000000 00000000 10101010
-------------------------------------------
rgb =   01101010 11111111 00001111 10101010
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

It is using the left shift operator to construct a bitmask to construct the ARGB value which is defined here

OldProgrammer
  • 12,050
  • 4
  • 24
  • 45