rickss has asked for the wisdom of the Perl Monks concerning the following question:
In this particular case, I have a value that is stored in a byte. The first (x0000000) bit represents whether the remaining bits (0xxxxxxx) are a positive or negative number from 1-128. If the value is negative (x0000000) = 1, the bits are flipped. (no, I don't know why they thought they needed to flip the bits as well as indicating if it was positive or negative).
I can tell whether I need to flip the bits (if the first bit is 1 or 0): $z = $x & (1 << 7);
but I can't quite get right the flipping of the remaining 7 bits into a number. My goal is getting back a decimal number that is the inverse of the right-most 7 bits.
I support I could look at the decimal value, and, if > 127, simply subtract 256 from it, but that seems inelegant.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Flipping partial bits
by jwkrahn (Abbot) on Nov 12, 2020 at 20:26 UTC | |
|
Re: Flipping partial bits
by roboticus (Chancellor) on Nov 12, 2020 at 20:55 UTC | |
|
Re: Flipping partial bits
by dave_the_m (Monsignor) on Nov 12, 2020 at 19:27 UTC | |
|
Re: Flipping partial bits
by haukex (Archbishop) on Nov 12, 2020 at 19:46 UTC | |
by jszinger (Scribe) on Nov 13, 2020 at 16:26 UTC | |
by haukex (Archbishop) on Nov 14, 2020 at 12:08 UTC | |
|
Re: Flipping partial bits
by ikegami (Patriarch) on Nov 13, 2020 at 16:39 UTC | |
|
Re: Flipping partial bits
by Marshall (Canon) on Nov 14, 2020 at 21:10 UTC |