Commit 60a1905
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
1 file changed
+1
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
3 | | - | |
| 2 | + | |
4 | 3 | | |
5 | 4 | | |
6 | 5 | | |
| |||
0 commit comments