473,542 Members | 2,829 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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!

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
kennydo
5 New Member
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

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

Similar topics

15
3306
by: alanbe | last post by:
Greetings I am making a flashcard type application to help me in my TCP/IP protocols test. My instructor will test us periodically on how a device or networking function relates to the OSI layer. EG. bits-layer 1. Any way, I want the quiz to reorder the problems each time I take it. Here is part of the code i did so far for 62 ...
2
2387
by: Zeb Davis | last post by:
Hello Newsgroup I'm new in C#, and I was wanting to create a console application involving generating 100 random numbers between 0 and 1000. After running that method, I want to write a method to test if the number is an even or odd number. After that I need to display in the console the smallest, largest, number of odds, and number of...
9
7020
by: Christopher Weaver | last post by:
I know that the bitwise AND of 8 and 4 will return 0 or false and the bitwise AND of 8 and 9 will return 1 or true but I don't know how to write the synax for it in C#. I have a value that ranges from 0 to 15 and I need to compare it to 15 in order to find if it contains the values 1, 2, 4, or 8. To represent it more graphically, the value...
2
1297
by: john conwell | last post by:
I have 3 5-digit numbers that i'd like to combine into one 15 digit number, and later rip back into the original 3 5-digit numbers. I know i can do this easily with some string manipulations, but i'm pretty sure i can do the same thing with some bitwise operation like bit shifting or something. basically, if i have the following 3 numbesr...
45
2386
by: bobalong | last post by:
Hi I'm have some problem understanding how JS numbers are represented internally. Take this code for an example of weirdness: var biggest = Number.MAX_VALUE; var smaller = Number.MAX_VALUE - 1;
5
5767
by: noridotjabi | last post by:
I'm learning to program in C and any tutorial or book that I read likes to briefly touch on birdies operators and then move on without giving any sort of example application of them. Call me what you will but I cannot seem to see the purpose for bitwise operators. Especially the operators bitwise OR ( | ) and bitwise AND ( & ), I'm just not...
5
3301
by: Gigs_ | last post by:
Can someone explain me bitwise expression? few examples for every expression will be nice x << y Left shift x >y Right shift x & y Bitwise AND x | y Bitwise OR x ^ y Bitwise XOR (exclusive OR) ~x Bitwise negation
2
4669
by: sudha30 | last post by:
hi please find code to add two numbers only by using bitwise operators
2
2712
by: mahi543 | last post by:
can we write a program in java using bitwise operators to add two numbers
7
1830
by: Jeff.Goldfinkle | last post by:
Hi All Is there a simple way to twiddle the bits of a float? In particular, I would like to round my float to the n most significant bits. For example - 0.123 in binary is 0.000111111 Rounding to 4 bits I get 0.0001. I can pack and unpack a float into a long e.g.
0
7333
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7576
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7723
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7673
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5874
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5250
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3383
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1803
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
624
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.