0

How can I get the last two bits from Int8?

I would like to convert 00110010 to 00000010.

Alexander
  • 59,041
  • 12
  • 98
  • 151
ObranS
  • 200
  • 2
  • 10

1 Answers1

3

Use the bitwise and operator, &:

let byte: Int8 = 0b0011_0010
let masked = byte & 0b0000_0011
print(String(masked, radix: 2)) // => 10
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Alexander
  • 59,041
  • 12
  • 98
  • 151