Add HasBitFlag, SetBitFlag, UnSetBitFlag

This commit is contained in:
殷勇 2023-08-25 16:17:22 +08:00
parent 8716d301c6
commit 9823133648

View File

@ -1,18 +1,18 @@
package q5
import (
"os"
"net"
"fmt"
"net/http"
"time"
"io/ioutil"
"reflect"
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"reflect"
"time"
)
func GetDaySeconds(seconds int64, timeZone int64) int64 {
return ((seconds + timeZone * 3600)/3600/24 + 1) * 3600 * 24 - 3600 * timeZone;
return ((seconds+timeZone*3600)/3600/24+1)*3600*24 - 3600*timeZone
}
func GetTickCount() int64 {
@ -46,15 +46,15 @@ func Request(r *http.Request, name string) string {
return ""
}
func Response(w* http.ResponseWriter, data string) {
func Response(w *http.ResponseWriter, data string) {
(*w).Write([]byte(data))
}
func ResponseOk(w* http.ResponseWriter) {
func ResponseOk(w *http.ResponseWriter) {
(*w).Write([]byte(`{"errcode":0, "errmsg":""}`))
}
func ResponseErr(w* http.ResponseWriter, errCode int32, errMsg string) {
func ResponseErr(w *http.ResponseWriter, errCode int32, errMsg string) {
rspObj := map[string]interface{}{}
rspObj["errcode"] = errCode
rspObj["errmsg"] = errMsg
@ -62,7 +62,7 @@ func ResponseErr(w* http.ResponseWriter, errCode int32, errMsg string) {
Response(w, string(jsonObj))
}
func ResponseInt32Ok(w* http.ResponseWriter, key string, val int32) {
func ResponseInt32Ok(w *http.ResponseWriter, key string, val int32) {
(*w).Write([]byte(fmt.Sprintf(`{"errcode":0, "errmsg":"", "%s":%d}`, key, val)))
}
@ -130,3 +130,29 @@ func FormatUnixDateEx(sec int64) string {
strTime := time.Unix(sec, 0).Format("20060102")
return strTime
}
const (
maxBitNum = 63 // 最大的有效位号,根据 int64 的位宽确定
)
func HasBitFlag(val int64, bitNum int32) bool {
if bitNum < 0 || bitNum > maxBitNum {
return false
}
mask := int64(1) << bitNum
return (val & mask) != 0
}
func SetBitFlag(val *int64, bitNum int32) {
if bitNum < 0 || bitNum > maxBitNum {
return
}
*val |= int64(1) << bitNum
}
func UnSetBitFlag(val *int64, bitNum int32) {
if bitNum < 0 || bitNum > maxBitNum {
return
}
*val &= ^(int64(1) << bitNum)
}