# Re: [ferret_users] Bitwise operations in Ferret

```Dear Bill,

I had the same problem when dealing with MERIS data.
I've used a repeat cycle to iteratively define a series of variables
(f1_"bit") that extract the value of each bit using decreasing powers of
2, the INT() and MOD() function.

Here is the code I wrote to extract the land,cloud,water and pcd's flags
from a 2D var - "mds" containing the real representation of 8bit flag
word.

let f1_7 = int(mds/2^7); let r1_7 = mod(mds,2^7)

repeat/range=6:0:-1/name=pw ( \
let f1_`pw` = int(r1_`pw+1`/ 2^`pw`);\
let r1_`pw` = mod(r1_`pw+1`,2^`pw`);\
)

let/tit=land  land  = if f1_7 then f1_7
let/tit=cloud cloud = if f1_6 then f1_6
let/tit=water water = if f1_5 then f1_5

let not_cloud = if cloud then 1/0 else 1

let/tit=pcd_1_13 pcd_1_13 = if f1_4 then f1_4
let/tit=pcd_14   pcd_14   = if f1_3 then f1_3
let/tit=pcd_15   pcd_15   = if f1_2 then f1_2
let/tit=pcd_16   pcd_16   = if f1_1 then f1_1
let/tit=pcd_17   pcd_17   = if f1_0 then f1_0

I hope that helps,

Regards,

Paulo.
------------------------------------------------
Paulo B. Oliveira
INRB / IPIMAR - Sea and Fisheries Research Lab.
Av. Brasilia
1449-006 Lisboa
Portugal
Tel.: +351 213027068

-------------------------------------------------------------
On Fri, 2009-04-17 at 15:06 -0700, Gustafson, William I wrote:
> Dear Ferreters,
>
> Are there some tricks to do bitwise operations in Ferret? I have some
> MODIS satellite data that has quality assurance (QA) information saved
> at the bit level. For example, for a particular byte of the QA data,
> bits 1 & 2 are used for the optical thickness confidence QA, and bits
> 6 & 7 are used for the effective radius confidence QA. I would like to
> do masking on particular settings of these bits. How can I do that?
>
> My search so far has not found anything. The AND and OR operators just
> treat 0 as false and non-zero as true—they do not operate at the bit
> level. I have pondered doing this with an external function, but that
> defeats the simplicity I am looking for. I need to hand of the final
> script to other users that would not necessarily have the know-how to
> install external functions on their machines.
>
> Thanks for any suggestions!
>
> -Bill
> ___________________________________________
> William I. Gustafson Jr., Ph.D.
> Scientist
> ATMOSPHERIC SCIENCES AND GLOBAL CHANGE DIVISION
>
> Pacific Northwest National Laboratory
> P.O. 999, MSIN K9-30
> Richland, WA  99352
> Tel: 509-372-6110
> William.Gustafson@xxxxxxx
> http://www.pnl.gov/atmospheric/staff/staff_info.asp?staff_num=5716

```