how to do bit complement in perl

Discussion in 'Perl Misc' started by IJALAB, Jan 9, 2008.

  1. IJALAB

    IJALAB Guest

    hi all,

    i have a file in which there are 8 byte hex values one followed by
    another. For example:
    2A414364
    00001DA9
    01A3F9DD
    3FFFF661
    00EE6670
    000011CF
    2BC43FD0
    3FEB0003
    3F7FFF40

    I need to check for the last bit in every data and if it being set,
    then the next data's 24 bits have to be inverted(assuming last bit is
    d31, starting from d26 to d7 needs to be inverted). For example:
    1st data 0x2A414364 has last bit as '0' so the next word can be
    retained as same.But, 00001DA9(2nd data), the last bit is '1' so the
    next data 01A3F9DD will have to be printed as 3e5c061d. so, for the
    upper set of data, the below data needs to be printed. how do i do bit
    complement in hex using perl.

    2a414364
    00001da9
    3e5c061d
    000009a1
    3f1199b0
    000011cf
    143bc010
    3feb0003
    00800080

    regards,
    bala
    IJALAB, Jan 9, 2008
    #1
    1. Advertising

  2. IJALAB

    smallpond Guest

    On Jan 9, 2:20 pm, IJALAB <> wrote:
    > hi all,
    >
    > i have a file in which there are 8 byte hex values one followed by
    > another. For example:
    > 2A414364
    > 00001DA9
    > 01A3F9DD
    > 3FFFF661
    > 00EE6670
    > 000011CF
    > 2BC43FD0
    > 3FEB0003
    > 3F7FFF40
    >
    > I need to check for the last bit in every data and if it being set,
    > then the next data's 24 bits have to be inverted(assuming last bit is
    > d31, starting from d26 to d7 needs to be inverted). For example:
    > 1st data 0x2A414364 has last bit as '0' so the next word can be
    > retained as same.But, 00001DA9(2nd data), the last bit is '1' so the
    > next data 01A3F9DD will have to be printed as 3e5c061d. so, for the
    > upper set of data, the below data needs to be printed. how do i do bit
    > complement in hex using perl.
    >
    > 2a414364
    > 00001da9
    > 3e5c061d
    > 000009a1
    > 3f1199b0
    > 000011cf
    > 143bc010
    > 3feb0003
    > 00800080
    >
    > regards,
    > bala


    Test low bit
    perl -e 'print hex( "2A414364") & 1 ? "odd" : "even";'
    even
    perl -e 'print hex( "00001DA9") & 1 ? "odd" : "even";'
    odd

    Complement whole word
    perl -e 'printf "%08x",~hex( "00001DA9");'
    ffffe256

    XOR with mask
    perl -e 'printf "%08x", 0x00ffff00 ^ hex( "01A3F9DD");'
    015c06dd
    smallpond, Jan 9, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mantorok Redgormor

    sign magnitude, ones complement, two's complement

    Mantorok Redgormor, Oct 5, 2003, in forum: C Programming
    Replies:
    8
    Views:
    8,569
    Glen Herrmannsfeldt
    Oct 8, 2003
  2. sarathy

    1's complement and 2's complement

    sarathy, Aug 1, 2006, in forum: C Programming
    Replies:
    20
    Views:
    2,165
    Bo Persson
    Aug 2, 2006
  3. sarathy
    Replies:
    22
    Views:
    2,310
    Bo Persson
    Aug 2, 2006
  4. salvador1985

    8 bit full adder 2's complement

    salvador1985, Apr 19, 2009, in forum: VHDL
    Replies:
    4
    Views:
    9,953
    salvador1985
    Apr 20, 2009
  5. Roberto Waltman

    2's complement vs. 1's complement vs. ...

    Roberto Waltman, Jun 9, 2011, in forum: C Programming
    Replies:
    4
    Views:
    1,332
    Michael Press
    Jun 14, 2011
Loading...

Share This Page