forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryString.java
More file actions
28 lines (25 loc) · 707 Bytes
/
BinaryString.java
File metadata and controls
28 lines (25 loc) · 707 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.rampatra.bits;
/**
* @author rampatra
* @since 2019-03-21
*/
public class BinaryString {
/**
* Returns the binary representation of a {@code byte}.
*
* @param b a byte.
* @return the binary representation of the input byte.
*/
private static String toBinaryString(byte b) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < Byte.SIZE; i++) {
sb.append(b & (byte) 1);
b >>= 1;
}
return sb.reverse().toString();
}
public static void main(String[] args) {
System.out.println(toBinaryString((byte) 0xff));
System.out.println(toBinaryString((byte) (0xff >> 3)));
}
}