# Help with bitwise operations

Hi Steve and Florian

I’ve been getting into using bitwise operations to save variables. Such a great feature!

There’s one type of operation, I can’t figure out, but I’m sure it can be done!

• I want to copy the status of bit 1-16 of gc to bit 1-16 of gk (and leave bit 17-32 of gk unchanged)
• Similarly, I want to copy the status of bit 1-16 of gc to bit 17-32 of gk (and leave bit 1-16 of gk unchanged)

I’m only able to figure out a very tedious and ineffective way of doing this. What would be the most “elegant” way to do this?

Best wishes from Denmark,
Mathias

Hi Mathias, welcome back!

Please find the attached example. Pay attention to the rules. I entered the numbers in hex when writing the translator but after saving the file MT Pro converts it to decimal so I put the original hex values in comments.

The first translator takes gc and masks the upper 16 bits and puts the result into pp

Then since we are going to be replacing them into the lower bits of gk, we also need to mask out the lower bits.

Finally we perform a bitwise OR operation to combine them into gk

The second translator is similar except we mask the upper bits of gk and we bit shift the masked results of pp.

Then we combine them into gk with a bitwise OR operation.

I also included log statements so that you can see what is happening in the Log window.

I hope this helps.

Steve Caldwell
Bome Customer Care

Also available for paid consulting services: bome@sniz.biz

Hi Steve!

Thanks for putting that together. I don’t understand it yet, but it works

One of the things I learned: I’ve been identifying the bits as 1-32, when it is actually 0-31 (?)
But curiosly, I haven’t had any issues, because rr is equal in both the below examples:

pp=oo>>0
rr=pp&1

pp=oo>>32
rr=pp&1

Because 32 is “a full circle back to square one”?

Thanks again! You’re a great help

Best, Mathias

Glad to help! Yes, it takes a little getting used to.

Steve Caldwell
Bome Customer Care

Also available for paid consulting services: bome@sniz.biz

Hi Steve,

Another lesson I learnt in this proces: Due to the sign bit, moving bits around in chunks can’t always be done without changing bits that aren’t moved.

Specifically, moving/mirroring bits 17-32 of one variable to bits 1-16 of another variable changes bits 17-32 of the latter, which wasn’t my intention.

It those cases, I had to mirror the bits individually in order to preserve the settings of the bits that wasn’t meant to change.

It took a while to figure out what was going on, but I found a solution

Best, Mathias

Yes, I was clearing the bits before putting the new bits in. You need to either use XOR to update the ones ore to perform a ones compliment and AND to Updates the zeros.

``````Setting a bit

pp=1<<xx - xx is bit number to set

ga=ga|pp

Clearing a bit

pp=1<<xx - xx is bit number to clear
pp=pp^-1
ga=ga&pp

``````

Steve Caldwell
Bome Customer Care

Also available for paid consulting services: bome@sniz.biz

I was using this:

``````// Mask 0x0000ffff
pp=gx&-65536
// clear lower of gy
gy=gy&-65536
// shift bits up from masked gx (now pp)
pp=pp<<16
// combine gy with masked value pp
gy=gy|pp
``````

It did not work as expected…