Do the exercises in the For the More Curious – Bit Manipulation section. Write a short paper (around 500 words) describing how the functions here might be useful, and proposing ways that you might utilize this functionality within a real program. Submit your paper here.
“For the More Curious: Bit Manipulation
Earlier, you saw that numbers have a binary representation. You can get the binary representation for a number at any time. For example, you could ask for the binary representation of the integer 42 with:
42.toString(radix = 2)
“101010”
(The radix parameter indicates the desired base of the outputted number. By specifying 2, you request a number in base 2, or binary. The default radix is 10, which outputs a decimal number. Another commonly used radix is 16, which returns a hexadecimal string.)
Kotlin includes functions for performing operations on the binary representation of a value, called bitwise operations – including operations you may be familiar with from other languages, such as Java, C, and JavaScript. Table 5.3 shows commonly used binary operations in Kotlin.
Table 5.3 Binary operations
Function Description Example
shl(bitcount) Shifts bits left by bitcount.
42.shl(2)
10101000
shr(bitcount) Shifts bits right by bitcount.
42.shr(2)
1010
inv() Inverts bits.
42.inv()
11111111111111111111111111010101
xor(number)
Compares two binary representations and performs a logical ‘exclusive or’ operation on the corresponding bit positions, returning 1 for each bit position that has a 1 in one input but not[…]”
Last Completed Projects
| topic title | academic level | Writer | delivered |
|---|
