The Value of 0xFF Number and Its Uses With & Operation in Java Article by Abdallah Sawan
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user