CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Bit Manipulation in PERL?

 



Maebius
Novice

Jan 3, 2001, 10:51 AM

Post #1 of 3 (407 views)
Bit Manipulation in PERL? Can't Post

Here is a quick question I am having trouble stepping through in my mind... How would you handle bit-manipulation of a single character in PERL?

I am writing a program that uses a 2 dimensional array to store information. Each "cell" of the resulting grid needs to have more than one possible "flag" or value set. (a somewhat strategic war-game, where example bits could be:
bit 000000001 = unmarked (hidden)
bit 000000002 = known
bit 000000004 = impassable
bit 000000008 = water (passible in some cases)
bit 000000010 = cloaked (hidden in most cases)

Thus a particular cell could be Known, Water, and Impassible (deep water?).

I am imagining the easiest way would be to have the array of integers setup, and each 'bit' of the integer represent various states or flags set.
Unfortunately, I am having trouble sorting through the math/algorithms involved before actually translating those thoughts to actual code.
Without a degree in mathematics, I am picturing converting each cell to a binary string 001010011 and then taking substrings of each position in the string?

Any thoughts or suggestions on an easier method of doing things? I searched the forums, but didn't find anything along these lines.


-Maebius
Living life -umop apisdn-
maebius@everthorn.net


perlplexer
Deleted

Jan 3, 2001, 11:29 AM

Post #2 of 3 (404 views)
Re: Bit Manipulation in PERL? [In reply to] Can't Post

1) instead of converting integers to strings you can use OR and AND logic operators to set/reset bits.

$a = 4; # 0000 0100
$a = $a | 0x01; # 0000 0100 OR 0000 0001 = 0000 0101
$a = $a & 0xFB; # 0000 0101 AND 1111 1011 = 0000 0001

2) This will require more memory but, hopefully, will be easier to understand

@earth = ([ {}, {} ], [ {}, {} ]);

$earth[0][0]{'water'} = 1; $earth[1][0]{'known'} = 1;
$earth[0][1]{'water'} = 1; $earth[1][0]{'known'} = 1;
$earth[1][0]{'water'} = 1; $earth[1][0]{'known'} = 1;
$earth[1][1]{'unknown'} = 1;

or

@earth = ([ [] , [] ], [ [], [] ]);

$earth[0][0][0] = 1; $earth[0][0][1] = 1;
$earth[0][1][2] = 1; $earth[0][1][1] = 1;
$earth[1][0][4] = 1; $earth[1][0][1] = 1;
$earth[1][1][0] = 1; $earth[1][1][1] = 0;

hope this helps...

--perlplexer



sturno
Deleted

Jan 26, 2001, 2:46 PM

Post #3 of 3 (388 views)
Re: Bit Manipulation in PERL? [In reply to] Can't Post

Howdy,

Here is a link for you to check. It contains a beta version of a data encryption shareware I am working on. There is lots of bit manipulation in there. You want in particular masking functions. That is to XOR an unknown against a known for a true or false result.

data encryption beta version
Don't spread this around yet. It works, but needs a few bells and whistles.

Enjoy.

It's nice to be important...
but it's more important to be nice.

(This post was edited by Jasmine on Jan 26, 2001, 6:34 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives