The Value of 0xFF Number and Its Uses With & Operation in Java Article by Abdallah Sawan
This commit is contained in:
parent
1a6ff515fc
commit
b2a3a69b4a
@ -0,0 +1,20 @@
|
|||||||
|
package com.baeldung.number_0xff;
|
||||||
|
|
||||||
|
public class Number0xff {
|
||||||
|
|
||||||
|
public static int getRedColor(int rgba) {
|
||||||
|
return rgba >> 24 & 0xff;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int getGreenColor(int rgba) {
|
||||||
|
return rgba >> 16 & 0xff;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int getBlueColor(int rgba) {
|
||||||
|
return rgba >> 8 & 0xff;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int getAlfa(int rgba) {
|
||||||
|
return rgba >> rgba & 0xff;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,49 @@
|
|||||||
|
package com.baeldung.number_0xff;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
public class Number0xffUtitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test0xFFAssignedToInteger() {
|
||||||
|
int x = 0xff;
|
||||||
|
int expectedValue = 255;
|
||||||
|
assertEquals(x, expectedValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test0xFFAssignedToByte() {
|
||||||
|
byte y = (byte) 0xff;
|
||||||
|
int expectedValue = -1;
|
||||||
|
assertEquals(x, expectedValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenColor_thenExtractRedColor() {
|
||||||
|
int rgba = 272214023;
|
||||||
|
int expectedValue = 64;
|
||||||
|
assertEquals(Number0xff.getRedColor(rgba), expectedValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenColor_thenExtractGreenColor() {
|
||||||
|
int rgba = 272214023;
|
||||||
|
int expectedValue = 57;
|
||||||
|
assertEquals(Number0xff.getGreenColor(rgba), expectedValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenColor_thenExtractBlueColor() {
|
||||||
|
int rgba = 272214023;
|
||||||
|
int expectedValue = 168;
|
||||||
|
assertEquals(Number0xff.getBlueColor(rgba), expectedValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenColor_thenExtractAlfa() {
|
||||||
|
int rgba = 272214023;
|
||||||
|
int expectedValue = 7;
|
||||||
|
assertEquals(Number0xff.getAlfa(rgba), expectedValue);
|
||||||
|
}
|
||||||
|
}
|
@ -1,23 +0,0 @@
|
|||||||
package com.baeldung;
|
|
||||||
|
|
||||||
public class Main {
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
int x = 0xff;
|
|
||||||
System.out.println(x); // output is 255
|
|
||||||
|
|
||||||
byte y = (byte) 0xff;
|
|
||||||
System.out.println(y); // output is -1
|
|
||||||
|
|
||||||
int rgba = 272214023;
|
|
||||||
int r = rgba >> 24;
|
|
||||||
int g = rgba >> 16 & 0xFF;
|
|
||||||
int b = rgba >> 8 & 0xFF;
|
|
||||||
int a = rgba & 0xFF;
|
|
||||||
|
|
||||||
System.out.println(r); // output is 64
|
|
||||||
System.out.println(g); // output is 57
|
|
||||||
System.out.println(b); // output is 168
|
|
||||||
System.out.println(a); // output is 7
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user