Skip to content

Instantly share code, notes, and snippets.

@CapsAdmin
Created January 20, 2022 11:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CapsAdmin/9fe57314ab9f77bec0445eb042ac56a5 to your computer and use it in GitHub Desktop.
Save CapsAdmin/9fe57314ab9f77bec0445eb042ac56a5 to your computer and use it in GitHub Desktop.
Synthesizing 10/10 solutions
=======
func swapBits(x int32) int32 {
return ((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1)
}
=======
func swapBits(x int32) int32 {
return ((x & 0xAAAAAAAA) >> 1) | ((x & 0x55555555) << 1)
}
=======
func swapBits(x int32) int32 {
return (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1))
}
=======
func swapBits(x int32) int32 {
return (x & 0x2AAAAAAA) >> 1 | (x & 0x55555555) << 1
}
=======
func swapBits(x int32) int32 {
x = x ^ (1 << 5)
x = x ^ (1 << 17)
return x
}
=======
func swapBits(x int32) int32 {
return (x & 0x9249249) | ((x & 0x4924924) >> 1)
}
=======
func swapBits(x int) int {
return ((x & 0x1) << 15) | ((x & 0x80000000) >> 15)
}
=======
func swapBits(x int32) int32 {
return ((x & 0xA0000000) >> 17) | ((x & 0x20000000) >> 15)
}
=======
func swapBits(x int) int {
mask := 1<<6 + 1<<17
x = x ^ mask
return x
}
=======
func swapBits(i int) int {
// Masking the first and second bit of the number
i = i & 0b0111111111111111
// Masking the seventh and eighteenth bit of the number
i = i & 0b10111111111111111
// Shifting the seventh bit to the first bit
i = i << 1
// Shifting the eighteenth bit to the second bit
i = i << 1
// Masking the first and second bit of the number
i = i & 0b0111111111111111
// Masking the seventh and eighteenth bit of the number
i = i & 0b10111111111111111
return i
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment