473,542 Members | 2,829 Online

# Hex Numbers and Bitwise AND

5 New Member
Can someone help me with the following problem.

I store the hex value in the varible called \$hexnum = 33D5;
print it out and I got 33D5 which is correct, but as soon as I and it with 3FFF (\$data = \$hexnum & 0x3FFF) and print it out. I got 33. so I put a print statement to print "\$hexnum\n" ; and I got 33. The question is why did my original value got change and where is the D5 goes? Thanks in advance for the help.

Kenny,
Feb 14 '07 #1
8 20192
docsnyder
88 New Member
@kennydoc
Expand|Select|Wrap|Line Numbers
1. \$hexnum = 03D5;
should be
Expand|Select|Wrap|Line Numbers
1. \$hexnum = 0x03D5;
Enjoy!

Greetz, Doc
Feb 14 '07 #2
kennydo
5 New Member
@kennydoc
Expand|Select|Wrap|Line Numbers
1. \$hexnum = 03D5;
should be
Expand|Select|Wrap|Line Numbers
1. \$hexnum = 0x03D5;
Enjoy!

Greetz, Doc

Thanks for the tips, but I have another question: my hexnum varible be come decimal number automaticly after I do a bitwise and.

\$hexnum = 0x33D5;

\$data = \$hexnum & 0x2FFF;

after bitwise and both \$data and 4hexnum become decimal number:

\$data = 9173;
\$hexnum = 13269;

Thanks again for all the help.

Kenny,
Feb 14 '07 #3
docsnyder
88 New Member
@kennydo

> my hexnum varible be come decimal number automaticly after I do a bitwise and.

How do you know about that? I guess you print the values. What is your "%"-formatting directive? Possibly "%d"?

Greetz, Doc
Feb 14 '07 #4
kennydo
5 New Member
@kennydo

> my hexnum varible be come decimal number automaticly after I do a bitwise and.

How do you know about that? I guess you print the values. What is your "%"-formatting directive? Possibly "%d"?

Greetz, Doc
I print them out using the follow comand:

print ("hexnum before bitwise and: \$hexnum\n");

print ("data after bitwise and: \$data\n");
print ("hexnum after bitwise and: \$hexnum\n");

the first print statement showed the correct hex value.
the two print statement after the bitwise show decimal values.

I know how to format the value so that it will print hex number, but I need to use these value for other calculation before getting the final result. Hope you can help point me to the right direction.

Thx,

Kenny,
Feb 15 '07 #5
docsnyder
88 New Member
@kennydo

There is only "one" internal representation of integer numbers in Perl. Numbers do *not* "become" decfimal or hex. Whether numbers "appear" as decimal or hex (or binary, etc.) just depends on the formatting (%) when printed.

As a consequence, you can do arithmetics with numbers as well as applying logical operators.

You have to distinguish the terms "value" and "representation ".

"\$n = 10;", "\$n = 0xa" and "\$n = \n" all result in the same value for \$n.

Why should "10 & 16" lead to another result as "0x0a & 0x0f"?

Hope this helps.

Greetz, Doc
Feb 15 '07 #6
kennydo
5 New Member
@kennydo

There is only "one" internal representation of integer numbers in Perl. Numbers do *not* "become" decfimal or hex. Whether numbers "appear" as decimal or hex (or binary, etc.) just depends on the formatting (%) when printed.

As a consequence, you can do arithmetics with numbers as well as applying logical operators.

You have to distinguish the terms "value" and "representation ".

"\$n = 10;", "\$n = 0xa" and "\$n = \n" all result in the same value for \$n.

Why should "10 & 16" lead to another result as "0x0a & 0x0f"?

Hope this helps.

Greetz, Doc

Thanks, This help a lot, but now I'm back to square one. If I assign a hex value to the varible the way you pointed out it work. But I'm trying to pop the value out of my two dimensional array and it not giving me the correct value (33 instead of 33D5). So when I try to and it with 0x2000 it take 33 and try to & it with 0x2000. I really appreciate the help.

Thx

Kenny,
Feb 15 '07 #7
docsnyder
88 New Member
Dear Kenny!

Explanation: Perl is par excellance type resistent, i.e. you do not have to declare variables as of beeing of a certain type. You can even turn a string into a number. But conversions like this reach their limit, when no more evidence is shown. For this reason, a conversion from a string to a number "breaks", as soon as a non-numerical character appears.

In your example, the "33D5" is reduced to "33", which indicates clearly, that you are trying to convert a hex string value into a number (the conversion has stopped as soon as the "non-numerical" character "D" has reached).

Use your hex-number as a "number", not as a string, and your script will recognize it thankfully.

Regards, Doc
Feb 15 '07 #8
kennydo
5 New Member
Dear Kenny!

Explanation: Perl is par excellance type resistent, i.e. you do not have to declare variables as of beeing of a certain type. You can even turn a string into a number. But conversions like this reach their limit, when no more evidence is shown. For this reason, a conversion from a string to a number "breaks", as soon as a non-numerical character appears.

In your example, the "33D5" is reduced to "33", which indicates clearly, that you are trying to convert a hex string value into a number (the conversion has stopped as soon as the "non-numerical" character "D" has reached).

Use your hex-number as a "number", not as a string, and your script will recognize it thankfully.

Regards, Doc
Thanks Doc,

I think you're right I store these number into the array with string format. I'll try to fing another way to strore these number. Again thanks for all the help.

Best regards,

Kenny,
Feb 16 '07 #9