ZBrushCentral

Bit Shifting Operations

I thought I saw a way to do bitshifting operations somewhere. But Now I cannot find them anywhere. Is it my imagination playing games and mixing up some other scripting language??? :rolleyes:

Well, If anyone knows a way to do this without calling an external Dll (or file) Please let me know.

Much Appriciated,

Chris Reid

Bitwise operations work fine in zscripting.

For example,
[Varset, mark, 4]
       [Ibutton, , ,
       [Note, [val, mark << 1]]
       ]
will display 8. I include a bracket around the bitwise operation, just to make them stand out as I do not often make use of them. As in:
[Varset, mark, 4]
       [Ibutton, , ,
       [Note, [val, (mark >> 1)]]
       ]
The above will display 2.

The other bitwise operators also work. | (OR), & (AND), ^ (XOR), ~ (NOT).

Mark what is the use of this?
Thanks
Andreseloy

There are many uses but bitwise operators become realy useful once you start to write zscripts that calculate numbers and need to do so quickly.

The operators are explained here.

You can write any zscript without using bitwise operators, they just make some things easier to accomplish, and also much faster to calculate.

I am sending you a PM later Andreseloy.

Thanks, I knew I saw them somewhere…:D:+1:

andreseloy-
They are also very usefull when you want to save memory and read an integer/char/long…etc value as many booleans. Each bit can be used as an on/off switch. for example:
you have an unsigned char that is 1 byte(8bits).
represented like this. 00000000 or 11111111 or any combo 1001011.
Each of these 0’s or 1’s can be your boolean on/off for something.

 00000001 would = 1, which may tell the script to set brush size.
 00000010 would = 2, which may tell the script to set the focus.
Now if your unsigned char would be 3 = 00000011, you would have 2 boolean values stored in one byte of info. Now hopefully you can see why this may be preferred in large quantities.
 havn't tested it in ZScript but here is how you would test it in C/C++.
 
 lets just say [color=Blue]unsigned char FlagVar = 3</span>, like our example above.
 00000011 = 3.
 if(FlagVar&1 == 1) //do something and then switch it off.
  {
  doSomethingFunc();
 (FlagVar) &= (~1);[color=Black]//you may need to look up the different operator values to see exactly what this means, but it is basically doing and AND opperation to the inverse of [color=MediumTurquoise]00000001~=11111110. This turns off the 1st bit.</span>
  }</span>
 then for testing the second bit it would be like this;
 if(FlagVar&(1&lt;&lt;1)==(1&lt;&lt;1))
  {
  doSomenthingElseFunc();
  (FlagVar) &= (~(1&lt;&lt;1));
  }
 
 Hope this helps. It does take some practice...;)
But once you get it you can really do some fast & fun stuff.
 
 Chris Reid

Thanks Mark and Chris, your support is very appreciated, this field has been long time see with terror by me but still very very beginning i enjoy now tons learning!
Andreseloy

Ah, now that is very interesting and many thanks. I can see that this could be very useful in certain circumstances. I understand the principle and I’ve played around with this with zscripts with a little success but can’t get a specific bit to return to 1. (For one thing, ZB doesn’t seem to like the ~ operator - at least, not in the ways I’ve been trying it.) If someone has had success could they please post an example?

You can use the eXclusive OR operator, ^, to toggle a single bit and leave the other bits as they were.

[VarSet, switch, 32]// 00100000
  
  [If, ButtonPressed = 3, [VarSet, switch, (switch ^ 1)]]// switch - 00100001
  [If, ButtonPressed = 4, [VarSet, switch, (switch ^ 2)]]// switch - 00100010
  [If, ButtonPressed = 5, [VarSet, switch, (switch ^ 4)]]// switch - 00100100
  
  [If, (switch & 1) = 1, [Note, "setting xxxx is On"]]
  [If, (switch & 2) = 2, [Note, "setting xxxx is On"]]
  [If, (switch & 4) = 4, [Note, "setting xxxx is On"]]
  

Ah, that works nicely. Thanks Mark.

But (according to my book) the complement operator (~) should toggle the values, whereas for me, using it always returns an error. Any thoughts?

EDIT: actually, your comments are for a switch initial value of 32! :slight_smile:

Just testing to see if you were awake :wink: