charlang/example/bitwise.char
Copy
// various ways to declare or assign value to a byte type b1 := byte(97) b2 := 'a' b3 := byte(b2) pl("b1: %#v, b2: %#v, b3: %#v", b1, b2, b3) plt(b1) plt(b2) plt(b3) // bitwise and b4 := b3 & 0x0F b5 := b3 & 0xF0 pl("%02X", b4) pl("%02X", b5) // Create a new byte pointer for scan functions // The new/newEx function also allocates space for it // Therefore, it can be assigned a value a := newEx("byte") setValueByRef(a, "3") plt(unref(a), toHex(unref(a))) // The sscanf function is similar to the sscanf function in other languages // Will scan from the first parameter (here is the string "a3") to match // The content of the second parameter '% x' format character is placed in the following variable a // Since a is a pointer of type byte, a3 is considered a byte integer in hexadecimal lowercase, i.e. 163 rs := sscanf("a3", "%x", a) plt(rs, unref(a)) // Dereference the value in variable a and place it in variable b b := unref(a) // Output the numbers in b as values, hexadecimal values, and binary values respectively pl("%v -> %x -> %b", b, b, b) // Invert the data in b bit by bit and place it in variable c c := bitNot(b) // // Output the numbers in c as values, hexadecimal values, and binary values respectively // // %08b represents the output of binary values. If there are less than 8 bits, 0 will be added before it pl("%v -> %X -> %08b", c, c, c) // Calculate and output the data in c and hexadecimal 0F (that is, 15 in Decimal, 00001111 in binary) bit by bit // Complex expression calculation is used here // The unhex instruction will decode a string into a byte list in hexadecimal format // Use the getItem instruction to retrieve the value of the first byte (with sequence number 0) in the list // Since 0F is a byte, the first byte is the value of the entire number tmp := unhex("0F") pl("%08b", c&tmp[0]) // The data in c and hexadecimal 0F (that is, 15 in Decimal, 00001111 in binary) are bitwise or calculated and output pl("%08b", c|tmp[0]) // The data in c and hexadecimal 0F (that is, 15 in Decimal, 00001111 in binary) are calculated by bit XOR and output pl("%08b", c^tmp[0])