Bit negation 
Author Message
 Bit negation


Quote:

>Hi,
>    I had a hex variable of the value 10
>        On negation of 10 I got F5 and not FE as one would expect

>        When 10 was negated using ~$hexvalue, I got 4294967285
>            which on converting to hex is FFFFFFF5.

>    The correct value of negating 10 should be FFFFFFFE...

>    The same when done for 04 gave the correct value of FFFFFFFB
>    Same holds true for 01, 02, 08 etc.. But not for 10,20,40,80 etc..

>Please let me know what am I doing wrong here...

I suspect you are wanting some automagical conversion, as negating 0xA
(decimal 10) gives FFFFFFF5.  You can use the hex function, or if the
values are literals at compile-time 0x as a prefix indicates to the
compiler that the following number is hexadecimal.

#!/usr/bin/perl

{
  print "enter number: ";
  $answer = <STDIN>;
  last unless defined $answer;

  $hex = hex $answer;

  printf "%08X negated is %08X\n", $hex, ~$hex;

  redo;

Quote:
}

__END__

seems to produce the answers you are after,

hope this helps,

Mike

--
The "usual disclaimers" apply.    | Meiko
Mike Stok                         | 130C Baker Ave. Ext

Meiko tel: (508) 371 0088 x124    |



Fri, 18 Jul 1997 20:50:13 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. Double bit-negation

2. Why no bit shifts on bit strings?

3. bit-twiddling on 32-bit machines

4. breaking bit vectors into smaller bit vectors

5. Conflicts between 32-bit and 64-bit

6. Bit-Shifting and 16-bit numbers

7. 32-bit perl module not recognized by 64-bit perl

8. Getting Perl Programs to print out bit by bit

9. Outputting a page bit by bit...

10. Proble with negation of sequence of characters using regular expression

11. negation in Regular Xpr.

12. Cannot modify integer negation (-) in predecrement (--)

 

 
Powered by phpBB® Forum Software