This is a very odd format, but another solution for you:
"~" is the complement operator, i.e. flip all bits
#!/usr/bin/perl use strict; use warnings; #assume each value is a perl var masked to lower 8 bits foreach (0x85, 0x05) { my $x = $_; #allow mod of $x in loop my $neg = $x & 0x80; #decide if negative $x = ~$x & 0x7F if $neg; #adjust if so printf "%s%-08b%s%d\n",($neg)?"-":" ",$x,($neg)?"-":" ",$x; } __END__ -1111010 -122 101 5
Update: I suppose many variations are possible:
Here $x winds up being the signed decimal value
#!/usr/bin/perl use strict; use warnings; #assume each value is a perl var masked to lower 8 bits foreach (0x85, 0x05) { my $x = $_; #allow mod of $x in loop my $neg = $x & 0x80; #decide if negative $x = ~$x & 0x7F, $x = -$x if $neg; #adjust if so printf "Input=%2x hex Input=%08b decimal=%d\n", $_, $_, $x; } __END__ Input=85 hex Input=10000101 decimal=-122 Input= 5 hex Input=00000101 decimal=5
Normal 2's complement notation could represent within 8 bits, an integer number from -128 to +127.
In the past computers have been designed with a "sign bit" followed by positive integer bits.
That failed miserably because there was the possibility of a "plus zero" and "minus zero".
This sign bit + one's complement is truly weird. I've never seen anything like it.

In reply to Re: Flipping partial bits by Marshall
in thread Flipping partial bits by rickss

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.