Fortune Telling Collection - Comprehensive fortune-telling - How to do bitwise operation in shell
How to do bitwise operation in shell
$((...))
$ echo $(( 3 * 4 )) 12
It is especially convenient in some scenarios, which can save the trouble of writing programs, such as verifying certain operations.
The following is the scenario of verifying deep understanding of computing system exercise 2. 12.
The expression ~0 will generate a mask of all 1, which is portable regardless of the word length of the machine.
$ printf " % x \ n " $((~ 0))ffffffffffffffff
$ printf " % # x \ n " $((~ 0))0x ffffffffffffffff
The above test shows that in the shell, the bitwise inversion of 0 is 64 bits.
Print the leading characters of the shell's printf command: Table 7-4 of the shell Script Learning Guide describes the meaning of the # symbol in the format parameters, and you can use the # to output the leading characters 0x (16 hexadecimal) and 0 (octal).
X & amp0xFF produces a value consisting of the least significant byte of x.
$ printf " % # x \ n " $((0x 89 abcdef & amp; 0xFF ))0xef
$ printf " % # . 8x \ n " $((0x 89 abcdef & amp; 0xFF ))0x000000ef
The following x = 0x8765432 1
The least significant byte and all other bits of A.x are set to 0.
$ printf " % # . 8x \ n " $((0x 8765432 1 & amp; 0xFF )) 0x0000002 1
$ printf " % # . 8x \ n " $((0x 8765432 1 & amp; ? bash:0x 8765432 1 & amp; ? 0xFF: Syntax error: Expected operand (error flag is "? 0xFF”)
The answer to the exercise given in the book is "X &;; ? 0xFF "、"? "Here you are. The number 1 verifies that the shell is not working properly.
B All positions except the least significant byte of X are complemented, and the least significant byte remains unchanged.
$ printf " % # x " $((0x 8765432 1 ^ ~ 0x ff))0x ffffffff 789 ABC 2 1
Because ~0xff generates a 64-bit mask, the result is somewhat unexpected, but the last 32 bits are expected.
The least significant byte of C.x is set to all 1, and all other bytes remain unchanged.
$ printf " % # x " $((0x 8765432 1 | 0x ff))0x 876543 ff
- Previous article:About "Game Writing Protocol on a Sunny Day"
- Next article:Ups and downs fortune telling _ ups and downs
- Related articles
- 1969 the fate of women born on the seventh day of the fifth lunar month.
- Fortune-telling auspicious words
- It belongs to the fate of the sheep in 2022 and the fortune of the zodiac sheep in his later years.
- Words describing fairies in novels
- Fortune-telling with Thumb _ Seeing Life with Thumb
- Julian Waghann fortune-telling geomantic omen
- I never tell my fortune _ What do you mean, I never tell my fortune?
- Buddhist fortune telling _ Buddhist fortune telling
- Fortune telling is a lie _ Is fortune telling a lie?
- Ask for additional photos of Jiazi Town in Lufeng (the more the better).