-
Notifications
You must be signed in to change notification settings - Fork 3
/
byte.functions
36 lines (30 loc) · 1.05 KB
/
byte.functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# This file is part of shellfire byte. It is subject to the licence terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/shellfire-dev/byte/master/COPYRIGHT. No part of shellfire byte, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
# Copyright © 2014-2015 The developers of shellfire byte. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/shellfire-dev/byte/master/COPYRIGHT.
byte_isBitSet()
{
return $(( 1 - $(_byte_bit "$@") ))
}
byte_printBinary()
{
local binaryValue
local bit=7
while [ $bit -ge 0 ]
do
binaryValue=${binaryValue}$(_byte_bit $1 $bit)
bit=$((bit-1))
done
printf '%s' $binaryValue
}
byte_setBit()
{
local binaryValue=$1
local bit=$2
printf '%s' $(( 1 << bit | binaryValue ))
}
_byte_bit()
{
local value=${1:-0}
local bit=${2-0}
local bitMask=$(( 1 << bit ))
printf '%s' $(( value & bitMask ? 1 : 0 ))
}