Skip to content

Commit e60dd8c

Browse files
authored
[int-bin-str] create numbers module; int-> binary string (#790)
1 parent 9b5f439 commit e60dd8c

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## Core Kotlin Strings
2+
3+
This module contains articles about core Kotlin numbers.
4+
5+
### Relevant Articles
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>core-kotlin-numbers</artifactId>
7+
<name>core-kotlin-numbers</name>
8+
<packaging>jar</packaging>
9+
10+
<parent>
11+
<groupId>com.baeldung</groupId>
12+
<artifactId>core-kotlin-modules</artifactId>
13+
<version>1.0.0-SNAPSHOT</version>
14+
</parent>
15+
16+
<dependencies>
17+
</dependencies>
18+
19+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.intToBinaryRepresentation
2+
3+
import org.junit.jupiter.api.Assertions.assertEquals
4+
import org.junit.jupiter.api.Test
5+
6+
fun Int.to32BitBinaryString(): String =
7+
toUInt().toString(2).padStart(Int.SIZE_BITS, '0')
8+
9+
class IntToBinaryRepresentationUnitTest {
10+
@Test
11+
fun `when using Integer's toBinaryString() then get expected result`() {
12+
assertEquals("10110", Integer.toBinaryString(22))
13+
assertEquals("101010", Integer.toBinaryString(42))
14+
assertEquals("11111111111111111111111111111110", Integer.toBinaryString(-2))
15+
}
16+
17+
@Test
18+
fun `when using kotlin's toString(2) then get expected result`() {
19+
assertEquals("10110", 22.toString(radix = 2))
20+
assertEquals("101010", 42.toString(2)) // we can omit the parameter name "radix"
21+
22+
assertEquals("-10", (-2).toString(2))
23+
assertEquals("11111111111111111111111111111110", (-2).toUInt().toString(2))
24+
25+
assertEquals("10110", 22.toUInt().toString(radix = 2))
26+
assertEquals("101010", 42.toUInt().toString(2))
27+
}
28+
29+
@Test
30+
fun `when using to32BitBinaryString then get expected result`() {
31+
assertEquals("00000000000000000000000000010110", 22.to32BitBinaryString())
32+
assertEquals("00000000000000000000000000101010", 42.to32BitBinaryString())
33+
assertEquals("11111111111111111111111111111110", (-2).to32BitBinaryString())
34+
}
35+
}

core-kotlin-modules/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
<module>core-kotlin-lang-oop</module>
5555
<module>core-kotlin-lang-oop-2</module>
5656
<module>core-kotlin-lang-oop-3</module>
57+
<module>core-kotlin-numbers</module>
5758
<module>core-kotlin-strings</module>
5859
<module>core-kotlin-strings-2</module>
5960
<module>core-kotlin-strings-3</module>

0 commit comments

Comments
 (0)