# INVERSE question

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
Hi All,

I am trying to learn Perl.

Would someone tell me if there are "logical" operations?

What I am trying to do is write a simple program
that will take a 16 bit Hex cardinal (Modula2 terminology
for an unsigned integer) from a terminal command line,
invert it, and then write it back as Hex to the terminal

By Inverse, I mean to flip the bits.

This is what I want to happen in hexadecimal
FF -->> 00
F0 -->> 0F
F8 -->> 07

If I am not mooching too much here, would someone
mind throwing a three line together for me on
how to do this?  And how to run it.

Many thanks,
-T

## Re: INVERSE question

Welcome

You are _NOT_ looking for logical operators ...

... but for bitwise operators.

However you don't find bitwise negotiation in the section with "Bitwise
....." but under "Symbolic Unary Operators":
Unary "~" performs bitwise negation [...]

jue

## Re: INVERSE question

On 07/08/2015 10:09 PM, J�rgen Exner wrote:

Interesting.  I have done some minor assembly coding.
INV, AND, OR, XOR, Shift.  Kind of all the same thing.

But, I have to learn the difference in Perl.

Can I do "bitwise" and "logical" on the same
variable?

Thank you for helping me with this.

## Re: INVERSE question

It's actually rather simple. For normal everyday use you would use
logical operators. Example
if (CONDITION1 and CONDITION2 or not CONDITION3)
Here the CONDITIONx are boolean values and you use logical operators to
combine then.
Furthermore in Perl any scalar value also has a boolean value, e.g. a
number that is not equal to zero or a string that is not empty and is
not equal to '0' all have a boolean value of 'true'.

Bitwise operators on the other hand are really only used for
nitty-gritty bit-level manipulation but pretty much never as a condition
in an 'if' or 'while' statement.

Yes, you can. In Perl the basic variables are called scalars to
distinguish them from compound variables like arrays and hashes.
And every scalar will be cast automatically into different types as
needed, i.e. if I define \$number = 1234, then I can still do
"text \$number foo" # cast into a string
if (\$number) # cast into a boolean
\$number + 3.1415 #cast into a real
\$number ^ 0xFA #apply bit mask 0xFA

jue

## Re: INVERSE question

On 07/09/2015 08:06 AM, J�rgen Exner wrote:

Thank you!

Eventually
\$number ^ 0xFA #apply bit mask 0xFA
was something I wanted to do too

that (192.168.30.xxx)

I am going to have fun!

## Re: INVERSE question

[...]

Almost certainly not. ^ is the exclusive-or operator and it's defined as

A    B    A^B
-------------------
0    0    0
0    1    1
1    0    1
1    1    0

Applying a bit mask would usually be

defined as

A    B    A&~B
--------------------
0    0    0
0    1    0
1    0    1
1    1    0

## Re: INVERSE question

On 07/08/2015 09:51 PM, T wrote:

This may seem like I am asking you to do my homework.
So please let me explain.  You can tell me all day how
to do something till you are blue in the face and I won't
get it.  But if you show me an example, the lights go off
instantly.  It is how I learn.  I have always been this way.

Now about my Algebra paper ...   :-)

## Re: INVERSE question

On 09.07.15 07:27, T wrote:

type

\$ perldoc -f vec
\$ man perlop # or point something at some HTML edition of it

Specifically, look for "Bitwise Or and Exclusive Or";
these talk about ^ and |, with examples.

## Re: INVERSE question

On 07/08/2015 11:17 PM, Georg Bauhaus wrote:

It is a lot to swallow. But, it has lots of examples!

Thank you for helping me with this!

-T

This is going to be a big transition from Modula 2
and bash script

## Re: INVERSE question

On 07/08/2015 11:17 PM, Georg Bauhaus wrote:

Just out of curiosity, is "vec" short for "vector"?

I see how to write out the result.  How would I read something
into the variable from the command line?

In the example:
my \$foo = '';

Does this define and create the variable "foo"?
vec(\$foo,  0, 32) = 0x5065726C;     # 'Perl'

And this tell Perl that "foo" 32 bits with an
offset of zero?

Back to
my \$foo = '';

Why the "my"?

I am coming from Modula2, where you have to declare
what everything is.  Do we declare variable in
Perl or are they one size fits all?

In Modula 2, variables declared inside subroutines (called
procedures) were only local to the subroutine.  Is the
"my" the same thing?

Many thanks,
-T

## Re: INVERSE question

Example actually using 16 bit:

[rw@doppelsaurus]/tmp#perl -e 'printf("%x\n", ~hex(\$ARGV[0]) & 0xffff)' f0
ff0f

hex(\$ARGV[0]) means 'parse the first command-line argument as hex number
and return the result', ~ is the bitwise negation operators, ie, the one
which 'flips the bits'. Depending on your platform, Perl integers are
either 32-bit or 64-bit. That's the reason for the & 0xffff which does a
bitwise and in order to get rid of everything which doesn't fit into 16
bits. printf("%x\n", ...) formats the first argument after the format
string as hex number and prints it followed by a newline.

## Re: INVERSE question

On 07/09/2015 08:17 AM, Rainer Weikusat wrote:

Made me look at it very closely.  :-)

Thank you!

## Re: INVERSE question

T,

In general, yes one could use bit operations like some have suggested but keeping the input
as a string and using "tr" (the translate function), you can easily handle numbers of any
size.  Try this:  (let me know if you have any questions about it)

#!/usr/bin/perl

print "Enter a number in hex:\t";
\$input = <STDIN>;                             # Get input
\$x = uc(\$input);                              # Lower case
chomp(\$x);                                    # Remove newline

# Check that the number is valid, exit if not
if (\$x !~ m/^[0-9A-F]+\$/) {
print "\$x is not a valid hex number.\n";
exit;
}

\$x =~ tr/0123456789ABCDEF/FEDCBA9876543210/;  # Reverse Bits
print "The inverse is: \t\$x\n";               # Print result

John Black

## Re: INVERSE question

[...]

[...]

[...]

JFTR: The pack and unpack functions can also deal with hex numbers and
the bit operators also work with binary strings of an arbitraty length,
ie, the

\$x =~ tr/0123456789ABCDEF/FEDCBA9876543210/;

can also be expressed as

\$x = unpack('H*', ~pack('H*', \$x));

which will work with letters of either case.

## Re: INVERSE question

On 07/09/2015 09:12 AM, Rainer Weikusat wrote:

Would you explain exactly what is going on in the above?

I see '"unpack" does the reverse of "pack"', but it is not
sinking in.

Thank you for helping me with this.

## Re: INVERSE question

pack('H*', \$x)

expects a string of hex digits as input and packs the corresponding
values nibble-by-nibble into a binary string. Applying ~ to that inverts
each bit of each byte. Lastly, unpack('H*', ...) turns the binary string
back into hex digits.

## Re: INVERSE question

rweikusat@mobileactivedefense.com says...

Enter a number in hex:  438
The inverse is:         bc7f

John Black

## Re: INVERSE question

What's your idea of the inverse of 0x4380?

## Re: INVERSE question

rweikusat@mobileactivedefense.com says...

Except that's not the number I entered.

John Black

## Re: INVERSE question

You don't really expect me to believe that you're so ignorant that you
wouldn't understand the meaning of 'nibble-by-nibble' and don't know
that a byte has eight bits and not four, do you?