mirror of
https://github.com/notwa/rc
synced 2024-11-13 19:49:03 -08:00
21 lines
773 B
Bash
Executable file
21 lines
773 B
Bash
Executable file
#!/usr/bin/env zsh
|
|
# compat: +ash +bash +dash +hush -ksh +mksh +oksh +osh +posh +yash +zsh
|
|
|
|
# though technically compatible with other shells,
|
|
# extra functionality is through zsh's extended arithmetic functions.
|
|
|
|
bin() { ### @-
|
|
### perform arithmetic using the shell and display the result as
|
|
### an unsigned 8-bit integer in binary.
|
|
### see also [`arith`](#arith) and [`hex`](#hex).
|
|
###
|
|
### ```
|
|
### $ bin 123
|
|
### 01111011
|
|
### ```
|
|
local a="$(($@))"
|
|
a="$(( (((((((((((((a + 0x0FFFFF80) & 0x1000007F) + 0x00FFFFC0) & 0x1100003F) + 0x000FFFE0) & 0x1110001F) + 0x0000FFF0) & 0x1111000F) + 0x00000FF8) & 0x11111007) + 0x000000FC) & 0x11111103) + 0x0000000E) & 0x11111111 ))"
|
|
printf "%08X\n" "$a"
|
|
}
|
|
|
|
[ -n "${preload+-}" ] || bin "$@"
|