ID:195065
 
//Title: dec2bin
//Credit to: Popisfizzy


/*
Simple procedure to convert decimal into binary,
using powers and bitwise math. 'e' means 'expanded'.
By default, this is false, meaning that only the
relevant bits will be shown. If it is true, it will
be extended to 16 bits.

*Note: This version will only work for digits less
than or equal to sixteen bits in length.
*/


proc/dec2bin(n, e = 0)
for(var/a = 0, (e ? (a < 16) : ((1 << a) <= n)), a ++)
. = "[((1 << a) & n) && 1 || 0][.]"

//Test Code:

mob/verb/unexpanded_dec2bin(n as num)
world << "[n] (base 10) = [dec2bin(n)] (base 2)"

mob/verb/expanded_dec2bin(n as num)
world << "[n] (base 10) = [dec2bin(n, 1)] (base 2, expanded)"