Skip to content

Commit 60a1905

Browse files
committed
feat: Add IntToBinary class demonstrating integer to binary and hex conversion
WHAT the code does: - Defines `IntToBinary` class with a `main()` method. - Declares integer `x = 5`. - Converts `x` to: - Binary string using `Integer.toBinaryString(x)`. - Hexadecimal string using `Integer.toHexString(x)`. - Prints both results. WHY this matters: - Demonstrates Java’s built-in `Integer` utility methods for number base conversions. - Reinforces how integers can be represented in different numeral systems. - Useful in debugging, bitwise operations, and low-level programming. HOW it works: 1. `Integer.toBinaryString(5)` → `"101"`. 2. `Integer.toHexString(5)` → `"5"`. Output: 101 5 Tips & gotchas: - These methods return **strings**, not numbers — cannot directly do arithmetic on results. - For uppercase hex letters (A–F), use `Integer.toHexString(x).toUpperCase()`. - Negative numbers are represented in **two’s complement** form for binary output. - For other bases, use `Integer.toString(x, radix)` with desired radix (2–36). Use-cases: - Educational example for **number system conversion**. - Useful in applications needing binary/hex representations (e.g., cryptography, compilers). - Debugging bitwise operations. - Prepping for coding interviews involving base conversions. Short key: class-inttobinary-hex-conversion Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 6db4804 commit 60a1905

File tree

1 file changed

+1
-2
lines changed

1 file changed

+1
-2
lines changed

Section10Methods/PracticeProblems/src/IntToBinary.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
public class IntToBinary {
2-
public static void main(String[] args)
3-
{
2+
public static void main(String[] args) {
43
int x = 5;
54
System.out.println(Integer.toBinaryString(x));
65
System.out.println(Integer.toHexString(x));

0 commit comments

Comments
 (0)