Command line utility to quickly get info about the hex, binary and decimal representations of numbers, and the most common operations on the bits.
pip install -r requirements.txt
Check that it works by running jco
with no arguments
c:\path\to\repo>jco
Available commands:
help: Help.
one: Info about one number: jco one <number>
two: Info about 2 numbers: jco two <a> <b>
n: Info about 1 or 2 numbers (calls jco one or jco two)
bf: jco bitfield <field:nbits,field:nbits,...,field:nbits> <num>
Available options (space-separated list of <option>=<value>):
('n', 'n_bits') -> Number of bits
('w', 'word_size') -> Word size
('o', 'overflow') -> Whether to overflow negative numbers
('f', 'format') -> Table format (compatible with tabulate package)
('m', 'msbfirst') -> Whether to put the most significant bit first in a bitstring
Then check that you can run some simple queries (jco
can be called from anywhere)
>jco 0x1877
╒════════════════════════╤═══════╤═══════╤═══════════════╕
│ │ Dec │ Hex │ Bin │
╞════════════════════════╪═══════╪═══════╪═══════════════╡
│ A │ 6263 │ 1877 │ 1100001110111 │
├────────────────────────┼───────┼───────┼───────────────┤
│ ~A (13 bits) │ 1928 │ 788 │ 0011110001000 │
├────────────────────────┼───────┼───────┼───────────────┤
│ twos_compl(A, 13 bits) │ 1929 │ 789 │ 0011110001001 │
├────────────────────────┼───────┼───────┼───────────────┤
│ popcount(A) │ 8 │ 008 │ 0000000001000 │
├────────────────────────┼───────┼───────┼───────────────┤
│ nbits(A) │ 13 │ 00d │ 0000000001101 │
├────────────────────────┼───────┼───────┼───────────────┤
│ A >> 4 │ 391 │ 187 │ 0000110000111 │
╘════════════════════════╧═══════╧═══════╧═══════════════╛
>jco 187 0x981 n=16 w=8
╒════════════════════════╤═══════════╤═══════════╤══════════════════╕
│ │ Dec │ Hex │ Bin │
╞════════════════════════╪═══════════╪═══════════╪══════════════════╡
│ A │ 187 │ 00bb │ 0000000010111011 │
├────────────────────────┼───────────┼───────────┼──────────────────┤
│ ~A (16 bits) │ 65348 │ ff44 │ 1111111101000100 │
├────────────────────────┼───────────┼───────────┼──────────────────┤
│ twos_compl(A, 16 bits) │ 65349 │ ff45 │ 1111111101000101 │
├────────────────────────┼───────────┼───────────┼──────────────────┤
│ popcount(A) │ 6 │ 0006 │ 0000000000000110 │
├────────────────────────┼───────────┼───────────┼──────────────────┤
│ nbits(A) │ 8 │ 0008 │ 0000000000001000 │
├────────────────────────┼───────────┼───────────┼──────────────────┤
│ A >> 8 │ 0 │ 0000 │ 0000000000000000 │
├────────────────────────┼───────────┼───────────┼──────────────────┤
│ B │ 2433 │ 0981 │ 0000100110000001 │
├────────────────────────┼───────────┼───────────┼──────────────────┤
│ ~B (16 bits) │ 63102 │ f67e │ 1111011001111110 │
├────────────────────────┼───────────┼───────────┼──────────────────┤
│ twos_compl(B, 16 bits) │ 63103 │ f67f │ 1111011001111111 │
├────────────────────────┼───────────┼───────────┼──────────────────┤
│ popcount(B) │ 4 │ 0004 │ 0000000000000100 │
├────────────────────────┼───────────┼───────────┼──────────────────┤
│ nbits(B) │ 12 │ 000c │ 0000000000001100 │
├────────────────────────┼───────────┼───────────┼──────────────────┤
│ B >> 8 │ 9 │ 0009 │ 0000000000001001 │
├────────────────────────┼───────────┼───────────┼──────────────────┤
│ A - B │ 59194 │ e73a │ 1110011100111010 │
├────────────────────────┼───────────┼───────────┼──────────────────┤
│ B - A │ 2246 │ 08c6 │ 0000100011000110 │
├────────────────────────┼───────────┼───────────┼──────────────────┤
│ A + B │ 2620 │ 0a3c │ 0000101000111100 │
├────────────────────────┼───────────┼───────────┼──────────────────┤
│ A | B │ 2491 │ 09bb │ 0000100110111011 │
├────────────────────────┼───────────┼───────────┼──────────────────┤
│ A & B │ 129 │ 0081 │ 0000000010000001 │
├────────────────────────┼───────────┼───────────┼──────────────────┤
│ A ^ B │ 2362 │ 093a │ 0000100100111010 │
├────────────────────────┼───────────┼───────────┼──────────────────┤
│ A << 8 + B │ 50305 │ c481 │ 1100010010000001 │
├────────────────────────┼───────────┼───────────┼──────────────────┤
│ B << 8 + A │ undefined │ undefined │ undefined │
├────────────────────────┼───────────┼───────────┼──────────────────┤
│ A &~ B │ 59 │ 003b │ 0000000000111011 │
├────────────────────────┼───────────┼───────────┼──────────────────┤
│ distance(A, B) │ 6 │ 0006 │ 0000000000000110 │
╘════════════════════════╧═══════════╧═══════════╧══════════════════╛
>jco bf sign:1,exponent:8,fraction:23 0xfff1a238
╒═════╤════════════╤════════════════╤═════════════════════════╕
│ │ sign [1] │ exponent [8] │ fraction [23] │
╞═════╪════════════╪════════════════╪═════════════════════════╡
│ Bin │ 1 │ 11111111 │ 11100011010001000111000 │
├─────┼────────────┼────────────────┼─────────────────────────┤
│ Dec │ 1 │ 255 │ 7447096 │
├─────┼────────────┼────────────────┼─────────────────────────┤
│ Hex │ 1 │ ff │ 71a238 │
╘═════╧════════════╧════════════════╧═════════════════════════╛
That's all it does!