This is a creation in Article, where the information may have evolved or changed.
1, Judge parity
For I: = 0; I < 100; i++ {
If I% 2 = = 0 { println (i)}if I & 0x1 = = 0 { println (i)}
}
2, swap two numbers
Func Swap (A, b *int) {
If *a^*b! = 0 { Temp: = *a *a = *b *b = temp}
}
Func Swap (A, b *int) {
If *a^*b! = 0 { *a = *a + *b *b = *a-*b *a = *a-*b}
}
Func Swap (A, b *int) {
If *a^*b! = 0 { *a ^= *b *b ^= *a *a ^= *b}
}
3, swap symbol
Func Excangesymbol (a int) (ret int) {
ret = ^a + 1return
}
4, Absolute value
Func calcabs (a int) (ret int) {
ret = (a ^ a>>31)-A>>31return
}
5, binary in reverse order
Func bytereverse (a int) int {
A = ((A & 0xAAAA) >> 1) | ((A & 0x5555) << 1) A = ((A & 0xCCCC) >> 2) | ((A & 0x3333) << 2) A = ((A & 0xf0f0) >> 4) | ((A & 0x0f0f) << 4) A = ((A & 0xff00) >> 8) | ((A & 0x00FF) << 8) return a
}
6, ask for the number of 1
Func calconenum (a int) int {
A = ((A & 0xAAAA) >> 1) + (A & 0x5555) A = ((A & 0xCCCC) >> 2) + (A & 0x3333) A = ((A & 0xF0 F0) >> 4) + (A & 0x0f0f) A = ((A & 0xff00) >> 8) + (A & 0x00FF) return a
}