in reply to Flipping partial bits

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.