Skip to content

Instantly share code, notes, and snippets.

@crazy2be
Created January 20, 2012 01:30
Show Gist options
  • Save crazy2be/1644386 to your computer and use it in GitHub Desktop.
Save crazy2be/1644386 to your computer and use it in GitHub Desktop.
Shifty bits.go
func WriteInt(wr io.Writer, num uint64) error {
var shifty uint = 64 - 8 // 64 bit integer mines 8 bit integer (byte)
// Our algorithm simply doesn't write anything for a value of zero, but we still need to write something as per the protocol. Handle it as a special case.
if num == 0 {
return writeByte(wr, 0x00)
}
sentFirst := false
for ; shifty < 64; shifty -= 7 {
b := byte((num & (0x7F << shifty)) >> shifty)
if b == 0 && !sentFirst {
continue
}
sentFirst = true
if shifty > 0 {
b = b | 0x80
}
err := writeByte(wr, b)
if err != nil {
return err
}
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment