469,314 Members | 2,138 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,314 developers. It's quick & easy.

Hex Numbers and Bitwise AND

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 19362
@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
@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
@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

> 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
@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

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
Dear Kenny!

I do not know about your code, but your posting implies, that you use your "hex numbers" as "strings".

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
Dear Kenny!

I do not know about your code, but your posting implies, that you use your "hex numbers" as "strings".

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

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by Christopher Weaver | last post: by
45 posts views Thread by bobalong | last post: by
5 posts views Thread by noridotjabi | last post: by
5 posts views Thread by Gigs_ | last post: by
7 posts views Thread by Jeff.Goldfinkle | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.