in reply to Flipping partial bits
Update: I suppose many variations are possible:#!/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
Normal 2's complement notation could represent within 8 bits, an integer number from -128 to +127.#!/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
|
|---|