Solved

Logical & and |

Posted on 1999-01-07
10
171 Views
Last Modified: 2010-03-05
Looking to do some arithmetic operation on a number to clear all the hex-bits of a number except the lower 4. For example:

The number '96237' is equal to the hex value '0001 77ed'. I want to just keep the lower 4 bits which in this case is '77ed'. Hex value '77ed' is 30701, the number I want. Sooo... How can I take the number '96237' (or any number) do some arithemetic to clear all the bits except the last 4, to end up with the value '96237';

$num = 96237;

HELP!  Thanks so much!!
0
Comment
Question by:rw263
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 5

Expert Comment

by:scrapdog
ID: 1207447
Logically 'and' the number with 65535.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1207448
$num &= 0xffff;
0
 

Author Comment

by:rw263
ID: 1207449
Ozo your idea worked! One more question, sheesh you good at this!

How do I turn on all the high bits?  That is leave the lower four hex bits alone and turn on the upper four hex bits?  for example: $num_second = 46071

46071 = 0000 B3F7
            FFFF B357 = 4294947831

so I want to go from 46071 to 4294947831, via a similar same math operation.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:scrapdog
ID: 1207450
Ozo's answer was the same as mine...oh well.

>How do I turn on all the high bits?  That is leave the lower >four hex bits alone and turn on the upper four hex bits?  for >example: $num_second = 46071

$num |= 0xffff0000;

0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1207451
>Ozo's answer was the same as mine...oh well.

This was not meant to criticize ozo...we must have posted at the exact same time, so neither of us had seen each other's posts.  What I meant was that "65535" is the same as "0xffff".
0
 

Author Comment

by:rw263
ID: 1207452
whoopsie in that case ya get the points scrappy :) reanswer!
0
 
LVL 5

Accepted Solution

by:
scrapdog earned 50 total points
ID: 1207453
Thanks.  Did you get $num |= 0xffff0000 to work?
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1207454
ozo probably gave you the better answer, so if you want to reject and give him/her the points, go ahead...
0
 

Author Comment

by:rw263
ID: 1207455
whoopsie in that case ya get the points scrappy :) reanswer!
0
 

Author Comment

by:rw263
ID: 1207456
yes
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Passing shell scripts to run on the SFTP cmd object 12 189
perl to display a matrix report for 3-dimenional array 3 73
Perl 101 11 85
Move Function in Perl Script 2 77
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question