Skip to content

Latest commit

 

History

History
80 lines (61 loc) · 1.4 KB

bitshifting.md

File metadata and controls

80 lines (61 loc) · 1.4 KB

Bit Shifting in Golang

The Go programming language supports several bitwise operators including the followings:

 &   bitwise AND
 |   bitwise OR
 ^   bitwise XOR
&^   AND NOT
<<   left shift
>>   right shift

Left Shifting

x := 7 //00000111
fmt.Printf("%08b\n", x)
fmt.Println(x, x<<1) //14
fmt.Println(x, x<<2) //28
fmt.Println(x, x<<3) //56
fmt.Println(x, x<<4) //112

Manual Left shifting

fmt.Printf("%d\n", 0b00000111) //7
fmt.Printf("%d\n", 0b00001110) //14
fmt.Printf("%d\n", 0b00011100) //28
fmt.Printf("%d\n", 0b00111000) //56
fmt.Printf("%d\n", 0b01110000) //112
fmt.Printf("%d\n", 0b11100000) //224

Right Shifting

fmt.Println(x, x>>1) //3  0b00000011
fmt.Println(x, x>>2) //1  0b00000001
fmt.Println(x, x>>3) //0  0b00000000
func add(a, b int64) int64 {

   for b != 0 {

   	// common bits of a and b go to carry
   	carry := a & b

   	// xor - sum bits where one is not set
   	a = a ^ b

   	// shift carry by 1
   	b = carry << 1
   }
   return a
}

func subtract(a, b int64) int64 {

   for b != 0 {

   	// common bits of a and b go to borrow
   	borrow := ^a & b

   	// xor - sum bits where one is not set
   	a = a ^ b

   	// shift borrow by 1
   	b = borrow << 1
   }
   return a
}

Resource