Created
January 20, 2012 01:30
-
-
Save crazy2be/1644386 to your computer and use it in GitHub Desktop.
Shifty bits.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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