From 1a6ff515fc96b84ac17a491d567ffaa06cb3271a Mon Sep 17 00:00:00 2001 From: AbdallahSawan Date: Thu, 15 Oct 2020 23:21:47 +0300 Subject: [PATCH 1/6] The Value of 0xFF Number and Its Uses With & Operation in Java Article by Abdallah Sawan --- .../src/main/java/com/baeldung/Main.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 java-numbers-5/src/main/java/com/baeldung/Main.java diff --git a/java-numbers-5/src/main/java/com/baeldung/Main.java b/java-numbers-5/src/main/java/com/baeldung/Main.java new file mode 100644 index 0000000000..dfc625809d --- /dev/null +++ b/java-numbers-5/src/main/java/com/baeldung/Main.java @@ -0,0 +1,23 @@ +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 + } +} From b2a3a69b4aa2ca7035f0f95db11fd1a9f6351679 Mon Sep 17 00:00:00 2001 From: AbdallahSawan Date: Thu, 22 Oct 2020 11:19:33 +0300 Subject: [PATCH 2/6] The Value of 0xFF Number and Its Uses With & Operation in Java Article by Abdallah Sawan --- .../com/baeldung/number_0xff/Number0xff.java | 20 ++++++++ .../number_0xff/Number0xffUnitTest.java | 49 +++++++++++++++++++ .../src/main/java/com/baeldung/Main.java | 23 --------- 3 files changed, 69 insertions(+), 23 deletions(-) create mode 100644 java-numbers-4/src/main/java/com/baeldung/number_0xff/Number0xff.java create mode 100644 java-numbers-4/src/test/java/com/baeldung/number_0xff/Number0xffUnitTest.java delete mode 100644 java-numbers-5/src/main/java/com/baeldung/Main.java diff --git a/java-numbers-4/src/main/java/com/baeldung/number_0xff/Number0xff.java b/java-numbers-4/src/main/java/com/baeldung/number_0xff/Number0xff.java new file mode 100644 index 0000000000..2828dccdb3 --- /dev/null +++ b/java-numbers-4/src/main/java/com/baeldung/number_0xff/Number0xff.java @@ -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; + } +} diff --git a/java-numbers-4/src/test/java/com/baeldung/number_0xff/Number0xffUnitTest.java b/java-numbers-4/src/test/java/com/baeldung/number_0xff/Number0xffUnitTest.java new file mode 100644 index 0000000000..6dc4d03c48 --- /dev/null +++ b/java-numbers-4/src/test/java/com/baeldung/number_0xff/Number0xffUnitTest.java @@ -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); + } +} diff --git a/java-numbers-5/src/main/java/com/baeldung/Main.java b/java-numbers-5/src/main/java/com/baeldung/Main.java deleted file mode 100644 index dfc625809d..0000000000 --- a/java-numbers-5/src/main/java/com/baeldung/Main.java +++ /dev/null @@ -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 - } -} From 506953bb567dc7038c060b8d7372aaa746a122f9 Mon Sep 17 00:00:00 2001 From: AbdallahSawan Date: Thu, 22 Oct 2020 13:53:45 +0300 Subject: [PATCH 3/6] The Value of 0xFF Number and Its Uses With & Operation in Java Article by Abdallah Sawan --- .../com/baeldung/number_0xff/Number0xffUnitTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/java-numbers-4/src/test/java/com/baeldung/number_0xff/Number0xffUnitTest.java b/java-numbers-4/src/test/java/com/baeldung/number_0xff/Number0xffUnitTest.java index 6dc4d03c48..e9f80c57d5 100644 --- a/java-numbers-4/src/test/java/com/baeldung/number_0xff/Number0xffUnitTest.java +++ b/java-numbers-4/src/test/java/com/baeldung/number_0xff/Number0xffUnitTest.java @@ -3,7 +3,7 @@ package com.baeldung.number_0xff; import org.junit.Test; import static org.junit.Assert.assertEquals; -public class Number0xffUtitTest { +public class Number0xffUnitTest { @Test public void test0xFFAssignedToInteger() { @@ -20,28 +20,28 @@ public class Number0xffUtitTest { } @Test - public void givenColor_thenExtractRedColor() { + public void givenColor_whenGetRedColor_thenExtractRedColor() { int rgba = 272214023; int expectedValue = 64; assertEquals(Number0xff.getRedColor(rgba), expectedValue); } @Test - public void givenColor_thenExtractGreenColor() { + public void givenColor_whenGetGreenColor_thenExtractGreenColor() { int rgba = 272214023; int expectedValue = 57; assertEquals(Number0xff.getGreenColor(rgba), expectedValue); } @Test - public void givenColor_thenExtractBlueColor() { + public void givenColor_whenGetBlueColor_thenExtractBlueColor() { int rgba = 272214023; int expectedValue = 168; assertEquals(Number0xff.getBlueColor(rgba), expectedValue); } @Test - public void givenColor_thenExtractAlfa() { + public void givenColor_whenGetAlfa_thenExtractAlfa() { int rgba = 272214023; int expectedValue = 7; assertEquals(Number0xff.getAlfa(rgba), expectedValue); From 06419d4deb253022edebf22a9efed21005fa5ae8 Mon Sep 17 00:00:00 2001 From: AbdallahSawan Date: Thu, 22 Oct 2020 14:00:10 +0300 Subject: [PATCH 4/6] The Value of 0xFF Number and Its Uses With & Operation in Java Article by Abdallah Sawan --- .../test/java/com/baeldung/number_0xff/Number0xffUnitTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java-numbers-4/src/test/java/com/baeldung/number_0xff/Number0xffUnitTest.java b/java-numbers-4/src/test/java/com/baeldung/number_0xff/Number0xffUnitTest.java index e9f80c57d5..64615ccee9 100644 --- a/java-numbers-4/src/test/java/com/baeldung/number_0xff/Number0xffUnitTest.java +++ b/java-numbers-4/src/test/java/com/baeldung/number_0xff/Number0xffUnitTest.java @@ -16,7 +16,7 @@ public class Number0xffUnitTest { public void test0xFFAssignedToByte() { byte y = (byte) 0xff; int expectedValue = -1; - assertEquals(x, expectedValue); + assertEquals(y, expectedValue); } @Test From 12b927e2aea4d550c7430d6a6f68e85d1a13a5e4 Mon Sep 17 00:00:00 2001 From: AbdallahSawan Date: Thu, 22 Oct 2020 14:11:18 +0300 Subject: [PATCH 5/6] The Value of 0xFF Number and Its Uses With & Operation in Java Article by Abdallah Sawan --- .../src/main/java/com/baeldung/number_0xff/Number0xff.java | 2 +- .../test/java/com/baeldung/number_0xff/Number0xffUnitTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/java-numbers-4/src/main/java/com/baeldung/number_0xff/Number0xff.java b/java-numbers-4/src/main/java/com/baeldung/number_0xff/Number0xff.java index 2828dccdb3..1708afb5a2 100644 --- a/java-numbers-4/src/main/java/com/baeldung/number_0xff/Number0xff.java +++ b/java-numbers-4/src/main/java/com/baeldung/number_0xff/Number0xff.java @@ -15,6 +15,6 @@ public class Number0xff { } public static int getAlfa(int rgba) { - return rgba >> rgba & 0xff; + return rgba & 0xff; } } diff --git a/java-numbers-4/src/test/java/com/baeldung/number_0xff/Number0xffUnitTest.java b/java-numbers-4/src/test/java/com/baeldung/number_0xff/Number0xffUnitTest.java index 64615ccee9..95a9349b49 100644 --- a/java-numbers-4/src/test/java/com/baeldung/number_0xff/Number0xffUnitTest.java +++ b/java-numbers-4/src/test/java/com/baeldung/number_0xff/Number0xffUnitTest.java @@ -22,7 +22,7 @@ public class Number0xffUnitTest { @Test public void givenColor_whenGetRedColor_thenExtractRedColor() { int rgba = 272214023; - int expectedValue = 64; + int expectedValue = 16; assertEquals(Number0xff.getRedColor(rgba), expectedValue); } From 44ff0f03f6e674dea7261d51b7004497b667ff22 Mon Sep 17 00:00:00 2001 From: AbdallahSawan Date: Mon, 2 Nov 2020 10:31:44 +0200 Subject: [PATCH 6/6] The Value of 0xFF Number and Its Uses With & Operation in Java Article by Abdallah Sawan --- .../com/baeldung/number_0xff/Number0xffUnitTest.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/java-numbers-4/src/test/java/com/baeldung/number_0xff/Number0xffUnitTest.java b/java-numbers-4/src/test/java/com/baeldung/number_0xff/Number0xffUnitTest.java index 95a9349b49..b0feaf8628 100644 --- a/java-numbers-4/src/test/java/com/baeldung/number_0xff/Number0xffUnitTest.java +++ b/java-numbers-4/src/test/java/com/baeldung/number_0xff/Number0xffUnitTest.java @@ -9,41 +9,41 @@ public class Number0xffUnitTest { public void test0xFFAssignedToInteger() { int x = 0xff; int expectedValue = 255; - assertEquals(x, expectedValue); + assertEquals(expectedValue, x); } @Test public void test0xFFAssignedToByte() { byte y = (byte) 0xff; int expectedValue = -1; - assertEquals(y, expectedValue); + assertEquals(expectedValue, y); } @Test public void givenColor_whenGetRedColor_thenExtractRedColor() { int rgba = 272214023; int expectedValue = 16; - assertEquals(Number0xff.getRedColor(rgba), expectedValue); + assertEquals(expectedValue, Number0xff.getRedColor(rgba)); } @Test public void givenColor_whenGetGreenColor_thenExtractGreenColor() { int rgba = 272214023; int expectedValue = 57; - assertEquals(Number0xff.getGreenColor(rgba), expectedValue); + assertEquals(expectedValue, Number0xff.getGreenColor(rgba)); } @Test public void givenColor_whenGetBlueColor_thenExtractBlueColor() { int rgba = 272214023; int expectedValue = 168; - assertEquals(Number0xff.getBlueColor(rgba), expectedValue); + assertEquals(expectedValue, Number0xff.getBlueColor(rgba)); } @Test public void givenColor_whenGetAlfa_thenExtractAlfa() { int rgba = 272214023; int expectedValue = 7; - assertEquals(Number0xff.getAlfa(rgba), expectedValue); + assertEquals(expectedValue, Number0xff.getAlfa(rgba)); } }