ubit

package
v0.0.0-...-ade9fc0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 10, 2020 License: MIT Imports: 4 Imported by: 0

README

ubit

To Binary String

Inspired by biu.

Convert integer to binary string.

fmt.Println(ubit.ToBinaryString(4))               // [00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000100]
fmt.Println(ubit.ToBinaryString(int8(5)))         // 00000101
fmt.Println(ubit.ToBinaryString(int16(9)))        // [00000000 00001001]
fmt.Println(ubit.ToBinaryString([]byte{1, 2, 3})) // [00000001 00000010 00000011]
f := float32(5.20)
s := ubit.ToBinaryString(f)
fmt.Println("s =", s) // [01000000 10100110 01100110 01100110]

var outf float32
ubit.ReadBinaryString(s, &outf)
fmt.Println("outf =", outf) // 5.2
Benchmark
$ go test -bench=.
goos: linux
goarch: amd64
pkg: github.com/MDGSF/utils/ubit
Benchmark_ToBinaryString-8               6593515               179 ns/op
Benchmark_ByteToBinaryBytes-8           100000000               10.0 ns/op
PASS
ok      github.com/MDGSF/utils/ubit     2.385s

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BinaryBytesToByte

func BinaryBytesToByte(bs []byte) byte

BinaryBytesToByte change bs to byte, len(bs) <= 8 bs = []byte{'0', '0', '0', '0', '0', '1', '0', '1'}, b = 5 bs = []byte{'1', '0', '1'}, b = 5

func BinaryStringToByte

func BinaryStringToByte(bs string) byte

BinaryStringToByte change bs to byte, len(bs) <= 8 bs = "00000101", b = 5 bs = "101", b = 5

func BinaryStringToBytes

func BinaryStringToBytes(s string) (bs []byte)

BinaryStringToBytes convert binary string to bytes s = "[00000001 00000010]", bs = []byte{1, 2},

func ByteToBinaryBytes

func ByteToBinaryBytes(b byte, bs []byte)

ByteToBinaryBytes change b to binary format, store in bs b = 5, bs = []byte{'0', '0', '0', '0', '0', '1', '0', '1'}

func ByteToBinaryString

func ByteToBinaryString(b byte) string

ByteToBinaryString change b to binary format b = 5, result = "00000101"

func BytesToBinaryString

func BytesToBinaryString(bs []byte) string

BytesToBinaryString change bs to binary format bs = []byte{1, 2}, result = "[00000001 00000010]"

func CountBits

func CountBits(x int) int

CountBits count bits of number x

func Div2

func Div2(x int) int

Div2 x / 2

func IsEven

func IsEven(x int) bool

IsEven 判断 x 是不是偶数

func IsOdd

func IsOdd(x int) bool

IsOdd 判断 x 是不是奇数

func IsPowerOfTwo

func IsPowerOfTwo(x int) bool

IsPowerOfTwo judege whether x is 2^n

func Negative

func Negative(x int) int

Negative 正数-->负数,负数-->正数

func ReadBinaryString

func ReadBinaryString(s string, v interface{}) (err error)

ReadBinaryString change binary string s to v

func Swap

func Swap(a, b int) (int, int)

Swap a, b = b, a

func ToBinaryString

func ToBinaryString(v interface{}) string

ToBinaryString change v to binary format

func Uint16ToBinaryString

func Uint16ToBinaryString(v uint16) string

Uint16ToBinaryString change uint16 to bianry format

func Uint32ToBinaryString

func Uint32ToBinaryString(v uint32) string

Uint32ToBinaryString change uint32 to bianry format

func Uint64ToBinaryString

func Uint64ToBinaryString(v uint64) string

Uint64ToBinaryString change uint64 to bianry format

Types

This section is empty.

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL