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:

  1. syntax of variable declarations
  2. convert j byte before adding c, since go lacks (by design) integer promotion conversions when doing arithmetic.
  3. 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

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -