tdo

Dicing the onion
git clone git@abtrout.com:tdo.git
Log | Files | Refs | README

bitwise.go (673B)


      1 package tdo
      2 
      3 import (
      4 	"log"
      5 	"math/bits"
      6 )
      7 
      8 func FlipSecondBits(b byte) byte {
      9 	return (b ^ 0b01010101) | (b & 0b10101010)
     10 }
     11 
     12 func CycleRight(b byte) byte {
     13 	return (b << 7) | (b >> 1)
     14 }
     15 
     16 func ParityCheck(b byte) bool {
     17 	p := bits.OnesCount8(b&0b11111110) % 2
     18 	return p == int(b%2)
     19 }
     20 
     21 func PackDataBytes(bs []byte) []byte {
     22 	if len(bs)%8 != 0 {
     23 		log.Fatalf("Failed to PackDataBytes: input slice of length %d, not a multiple of 8", len(bs))
     24 	}
     25 	var out []byte
     26 	for i := 0; i < len(bs)/8; i++ {
     27 		for j := 0; j < 7; j++ {
     28 			k := i*8 + j // index in bs
     29 			left := (bs[k] >> 1) << (j + 1)
     30 			right := bs[k+1] >> (7 - j)
     31 			out = append(out, left|right)
     32 		}
     33 	}
     34 	return out
     35 }