Link to home
Start Free TrialLog in
Avatar of rw263
rw263

asked on

Logical & and |

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!!
Avatar of scrapdog
scrapdog
Flag of United States of America image

Logically 'and' the number with 65535.
Avatar of ozo
$num &= 0xffff;
Avatar of rw263
rw263

ASKER

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.
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;

>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".
Avatar of rw263

ASKER

whoopsie in that case ya get the points scrappy :) reanswer!
ASKER CERTIFIED SOLUTION
Avatar of scrapdog
scrapdog
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ozo probably gave you the better answer, so if you want to reject and give him/her the points, go ahead...
Avatar of rw263

ASKER

whoopsie in that case ya get the points scrappy :) reanswer!
Avatar of rw263

ASKER

yes