Skip to content

Instantly share code, notes, and snippets.

@fdncred
Last active August 4, 2022 02:14
Show Gist options
  • Save fdncred/6f5fb149c014c6de039401a6a07d3046 to your computer and use it in GitHub Desktop.
Save fdncred/6f5fb149c014c6de039401a6a07d3046 to your computer and use it in GitHub Desktop.
new nushell commands

Introducing the bytes and the bits command.

Commands specifically made for finding and manipulating binary data. Since naming is hard, we may end up changing the name. :)

the bytes command

potential crates for bytes

  • bytes query the bytes of a buffer

  • bytes at maybe thought of like str sub-string?

    • returns the byte at a specified offset in decimal 10 or hex 0x10
    • returns the bytes in a range if specified like 1..10
  • bytes index-of aka bytes contains

    • finds the index of a specified hex byte 0x10
    • finds the index of a specified byte buffer
  • bytes to-str - point to decode utf-8

    • returns specified bytes as a utf-8 string
  • bytes build

    • kind of like buildstring but for bytes
  • bytes size aka bytes length

    • return the size of something in bytes
  • bytes collect

    • collect bytes like str collect
  • bytes ends-with

    • returns bool of whether a buffer ends with specified byte(s)
  • bytes find-replace aka bytes replace

    • find and replace specified bytes
  • bytes add

    • add specified bytes to the --start, --end, --index
  • bytes remove

    • remove specified bytes from --start, --end, --index
  • bytes reverse

    • reverse a buffer of bytes
  • bytes starts-with

    • returns bool of whether a buffer starts with specified byte(s)

the bits command

protentional crates for bits

  • bits shl aka shift --left aka << -bitwise shift left moves bits left

  • bits shr aka shift --right aka >> -bitwise shift right moves bits right

  • bits rol aka rotate --left

    • bitwise shift left move a bit left and adds it to the right
  • bits ror aka rotate --right

    • bitwise shift right move a bit right and adds it to the left
  • bits and aka &

    • bitwise and performas logical and operation on each pair of bits
  • bits or aka |

    • bitwise or performs logical or operation on each pair of bits
  • bits xor aka ^

    • bitwise xor perofrms logical exclusive or operation on each pair of bits
  • bits not aka ~ aka complement

    • performs logical negation on each bit

TBD - we could add later or make params or leave out and there are many more we could add

  • sal - shift arithmatic left
  • sar - shift arithmatic right
  • rcl - rotate carry left
  • rcr - rotate carry right
  • shld - shift left number of bits
  • shrd - shift right number of bits
@sophiajt
Copy link

I was wondering if we should use pseudo-opcodes. Maybe it's just me, but I tend to think in them :D

bits shl, bits shr, bits rol, bits ror

@fdncred
Copy link
Author

fdncred commented Feb 11, 2022

these are the only ones i've ever really used <<, >>, &, |, ^, and ~.

@sholderbach
Copy link

How would we define the word size/wrapping condition/endianness for the bit twiddling commands?

@fdncred
Copy link
Author

fdncred commented Mar 2, 2022

@sholderbach Regarding endianness, we did something like this in the nushell into binary command

fn int_to_endian(n: i64) -> Vec<u8> {
    if cfg!(target_endian = "little") {
        n.to_le_bytes().to_vec()
    } else {
        n.to_be_bytes().to_vec()
    }
}

but there's also a sort-of-wrapper also that is to_ne_bytes(). I'm hopeful that this would work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment