Merge pull request #10170 from abdallahsawan/BAEL-4161

The Value of 0xFF Number and Its Uses With & Operation in Java Ar…
This commit is contained in:
Jonathan Cook 2020-11-02 19:33:34 +01:00 committed by GitHub
commit 678abbc8c5
2 changed files with 69 additions and 0 deletions

View File

@ -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 & 0xff;
}
}

View File

@ -0,0 +1,49 @@
package com.baeldung.number_0xff;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class Number0xffUnitTest {
@Test
public void test0xFFAssignedToInteger() {
int x = 0xff;
int expectedValue = 255;
assertEquals(expectedValue, x);
}
@Test
public void test0xFFAssignedToByte() {
byte y = (byte) 0xff;
int expectedValue = -1;
assertEquals(expectedValue, y);
}
@Test
public void givenColor_whenGetRedColor_thenExtractRedColor() {
int rgba = 272214023;
int expectedValue = 16;
assertEquals(expectedValue, Number0xff.getRedColor(rgba));
}
@Test
public void givenColor_whenGetGreenColor_thenExtractGreenColor() {
int rgba = 272214023;
int expectedValue = 57;
assertEquals(expectedValue, Number0xff.getGreenColor(rgba));
}
@Test
public void givenColor_whenGetBlueColor_thenExtractBlueColor() {
int rgba = 272214023;
int expectedValue = 168;
assertEquals(expectedValue, Number0xff.getBlueColor(rgba));
}
@Test
public void givenColor_whenGetAlfa_thenExtractAlfa() {
int rgba = 272214023;
int expectedValue = 7;
assertEquals(expectedValue, Number0xff.getAlfa(rgba));
}
}