import { Bits } from '../types' ; import { p2 } from '../utilities' ; import not from '../bits/not' ; import read from './read' ; example param { Buffer } buffer param { Number } length param { Number } offset return { Number } export default ( buffer , offset = 0 , length = 8 ) : number => { const bits = read ( buffer , offset , length ) ; if ( bits [ 0 ] === 0 ) { let result = 0 ; for ( let i = 0 ; i < length ; i ++ ) if ( bits [ i ] ) result += p2 [ length - i - 1 ] ; return result ; } else { let result = - 1 ; const inverted = not ( bits ) ; for ( let i = 0 ; i < length ; i ++ ) if ( inverted [ i ] ) result -= p2 [ length - i - 1 ] ; return result ; } } ;