MCPcopy Create free account
hub / github.com/distributedio/titan / GetBit

Function GetBit

command/strings.go:516–544  ·  view source on GitHub ↗

GetBit gets the bit at offset in the string value stored at key.

(ctx *Context, txn *db.Transaction)

Source from the content-addressed store, hash-verified

514
515// GetBit gets the bit at offset in the string value stored at key.
516func GetBit(ctx *Context, txn *db.Transaction) (OnCommit, error) {
517 key := []byte(ctx.Args[0])
518 offset, err := strconv.Atoi(ctx.Args[1])
519 if err != nil || offset < 0 {
520 return nil, ErrBitOffset
521 }
522
523 str, err := txn.String(key)
524 if err != nil {
525 if err == db.ErrTypeMismatch {
526 return nil, ErrTypeMismatch
527 }
528 return nil, errors.New("ERR " + err.Error())
529 }
530
531 if !str.Exist() {
532 return Integer(ctx.Out, 0), nil
533 }
534
535 val, err := str.GetBit(offset)
536 if err != nil {
537 return nil, errors.New("ERR " + err.Error())
538 }
539
540 if val != 0 {
541 return Integer(ctx.Out, 1), nil
542 }
543 return Integer(ctx.Out, 0), nil
544}
545
546// BitCount counts the number of set bits (population counting) in a string.
547func BitCount(ctx *Context, txn *db.Transaction) (OnCommit, error) {

Callers

nothing calls this directly

Calls 5

IntegerFunction · 0.85
GetBitMethod · 0.80
ErrorMethod · 0.65
ExistMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected