Last active
September 12, 2020 01:13
-
-
Save rileyjshaw/d4fb76d52141b1adaa5850931ce18df5 to your computer and use it in GitHub Desktop.
Quick silly scratchpad for swapping bits. Used as a quick generator for my alphanumeric display library.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function swap(str, a, b) { | |
const ceil = str.length - 1; | |
a = Math.min(Math.max(0, a), str.length - 1) || 0; | |
b = Math.min(Math.max(0, b), ceil) || 0; | |
if (a === b) return str; | |
if (a > b) [a, b] = [b, a]; | |
return ( | |
str.slice(0, a) + str[b] + str.slice(a + 1, b) + str[a] + str.slice(b + 1) | |
); | |
} | |
var swapPairs = [ | |
[0, 1], | |
[1, 2], | |
[5, 7], | |
[3, 11], | |
[1, 12], | |
[6, 9], | |
[8, 15], | |
[10, 4], | |
[7, 14], | |
[14, 11], | |
[6, 15], | |
[13, 15], | |
]; | |
console.log( | |
[ | |
" 0b1001000011111111, // 0", | |
" 0b0001000000001100, // 1", | |
" 0b0000001101110111, // 2", | |
" 0b0000001000111111, // 3", | |
" 0b0000001110001100, // 4", | |
" 0b0000011000111011, // 5", | |
" 0b0000001111111011, // 6", | |
" 0b0000000000001111, // 7", | |
" 0b0000001111111111, // 8", | |
" 0b0000001110111111, // 9", | |
" 0b0000001111001111, // A", | |
" 0b0100101000111111, // B", | |
" 0b0000000011110011, // C", | |
" 0b0100100000111111, // D", | |
" 0b0000000111110011, // E", | |
" 0b0000000111000011, // F", | |
" 0b0000001011111011, // G", | |
" 0b0000001111001100, // H", | |
" 0b0100100000110011, // I", | |
" 0b0000000001111100, // J", | |
" 0b0011000111000000, // K", | |
" 0b0000000011110000, // L", | |
" 0b0001010011001100, // M", | |
" 0b0010010011001100, // N", | |
" 0b0000000011111111, // O", | |
" 0b0000001111000111, // P", | |
" 0b0010000011111111, // Q", | |
" 0b0010001111000111, // R", | |
" 0b0000001110111011, // S", | |
" 0b0100100000000011, // T", | |
" 0b0000000011111100, // U", | |
" 0b1001000011000000, // V", | |
" 0b1010000011001100, // W", | |
" 0b1011010000000000, // X", | |
" 0b0000001110111100, // Y", | |
" 0b1001000000110011, // Z", | |
" 0b0100000101110000, // a", | |
" 0b0100000111010000, // b", | |
" 0b0000000101010000, // c", | |
" 0b0100001000101100, // d", | |
" 0b1000000101010000, // e", | |
" 0b0100101100000010, // f", | |
" 0b0100100110010001, // g", | |
" 0b0100000111000000, // h", | |
" 0b0100000000000000, // i", | |
" 0b0100100001010000, // j", | |
" 0b0111100000000000, // k", | |
" 0b0000000011000000, // l", | |
" 0b0100001101001000, // m", | |
" 0b0100000101000000, // n", | |
" 0b0100000101010000, // o", | |
" 0b0000100111000001, // p", | |
" 0b0100100110000001, // q", | |
" 0b0000000101000000, // r", | |
" 0b0100000110010001, // s", | |
" 0b0000000111010000, // t", | |
" 0b0100000001010000, // u", | |
" 0b1000000001000000, // v", | |
" 0b1010000001001000, // w", | |
" 0b1011010000000000, // x", | |
" 0b0000101000101100, // y", | |
" 0b1000000100010000, // z", | |
] | |
.map(line => | |
swapPairs.reduce( | |
(acc, swapPair) => swap(acc, ...swapPair.map(i => 19 - i)), // i + 4 for string start offset, 15 - i to flip MSB<>LSB. | |
line | |
) | |
) | |
.join("\n") | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code swaps us from the beautiful:
…to the actual: