go - Golang bitwise operations as well as general byte manipulation -
i have c# code performs bitwise operations on byte. trying same in golang having difficulties.
example in c#
byte a, c; byte[] data; int j; c = data[j]; c = (byte)(c + j); c ^= a; c ^= 0xff; c += 0x48;
i have read golang cannot perform bitwise operations on byte type. therefore have modify code type uint8 perform these operations? if there clean , correct/standard way implement this?
go can bitwise operations on byte
type, alias of uint8
. changes had make code were:
- syntax of variable declarations
- convert
j
byte
before addingc
, since go lacks (by design) integer promotion conversions when doing arithmetic. - removing semicolons.
here go
var a, c byte var data []byte var j int c = data[j] c = c + byte(j) c ^= c ^= 0xff c += 0x48
if you're planning bitwise-not in go, note operator ^
, not ~
used in other contemporary programming languages. same operator used xor, 2 not ambiguous, since compiler can tell which determining whether ^
used unary or binary operator.
Comments
Post a Comment