Wednesday, July 9, 2014

Bit shift operators

Experimentation in Java Snippet Runner to learn about bit shift operator and binary operators based on http://stackoverflow.com/questions/2534116/how-to-convert-get-rgbx-y-integer-pixel-to-colorr-g-b-a-in-java. >>> shifts all bits to the right with the bits falling off the right and zero filling from the left (as opposed to >> which fills from left based on sign (in this case 1 ... 0xAABBCCDD = -1430532899).


int argb = 0xAABBCCDD;
int r = (argb)&0xFF;
int g = (argb>>8)&0xFF;
int b = (argb>>16)&0xFF;
int a = (argb>>24)&0xFF;
int r_ = (argb)&0xFF;
int g_ = (argb>>8)&0xFF;
int b_ = (argb>>16)&0xFF;
int a_ = (argb>>24)&0xFF;
int r_1 = (argb)&0xFFFFFFFF;
int g_1 = (argb>>8)&0xFFFFFFFF;
int b_1 = (argb>>16)&0xFFFFFFFF;
int a_1 = (argb>>24)&0xFFFFFFFF;
System.out.println(Integer.toBinaryString(argb));
System.out.println(r + " red " + Integer.toBinaryString(r_) + "\n" + Integer.toBinaryString(r_1));
System.out.println(g + " green " + Integer.toBinaryString(g_) + "\n" + Integer.toBinaryString(g_1));
System.out.println(b + " blue " + Integer.toBinaryString(b_) + "\n" + Integer.toBinaryString(b_1));
System.out.println(a + " alpha " + Integer.toBinaryString(a_) + "\n" + Integer.toBinaryString(a_1));
System.out.println("righters");
System.out.println((argb>>8));
System.out.println((argb>>16));
System.out.println((argb>>24));

System.out.println(argb +"color methods");
   java.awt.Color c = new java.awt.Color(argb, true);
    System.out.println("red " + c.getRed());
    System.out.println("green " + c.getGreen());
    System.out.println("blue " + c.getBlue());
    System.out.println("alpha "  + c.getAlpha());

10101010101110111100110011011101
221 red 11011101
10101010101110111100110011011101
204 green 11001100
11111111101010101011101111001100
187 blue 10111011
11111111111111111010101010111011
170 alpha 10101010
11111111111111111111111110101010
righters
-5588020
-21829
-86
-1430532899color methods
red 187
green 204
blue 221
alpha 170

OK

No comments:

Post a Comment