Skip to content

Instantly share code, notes, and snippets.

@oguz-ismail
Last active August 29, 2022 12:42
Show Gist options
  • Save oguz-ismail/a3721dc71723ded5dc7a282367457693 to your computer and use it in GitHub Desktop.
Save oguz-ismail/a3721dc71723ded5dc7a282367457693 to your computer and use it in GitHub Desktop.
number to binary
LC_ALL=C
case $1 in
''|[!0-9]*|[!0]*[!0-9]*|0[!0-7Xx]*|0[Xx]|0[!Xx]*[!0-7]*|0[Xx]*[!0-9A-Fa-f]*)
printf '%s: invalid number: %s\n' "$0" "$1" >&2
exit 1
esac
x=$(($1)) bx=
while test $x -gt 0; do
bx=$((x & 1))$bx x=$((x >> 1))
done
printf '%s\n' ${bx:-0}
# vim: ft=sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment