1. #1
    Senior Member User Gallery
    Join Date
    May 2004
    Location
    CA
    Posts
    140

    Default 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???

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

    Much Appriciated,

    Chris Reid

  2. #2
    Senior Member User Gallery
    Join Date
    Jul 2002
    Location
    Copenhagen
    Age
    39
    Posts
    2,464

    Default

    Bitwise operations work fine in zscripting.

    For example,
    Code:
    [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:
    Code:
    [Varset, mark, 4]
           [Ibutton, , ,
           [Note, [val, (mark >> 1)]]
           ]
    The above will display 2.

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

  3. #3
    Senior Member User Gallery
    Join Date
    Jun 2001
    Location
    Venezuela
    Age
    63
    Posts
    6,455

    Default

    Mark what is the use of this?
    Thanks
    Andreseloy

  4. #4
    Senior Member User Gallery
    Join Date
    Jul 2002
    Location
    Copenhagen
    Age
    39
    Posts
    2,464

    Default

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

  5. #5
    Senior Member User Gallery
    Join Date
    May 2004
    Location
    CA
    Posts
    140

    Default

    Thanks, I knew I saw them somewhere...
    ...........................
    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 unsigned char FlagVar = 3, like our example above.
    00000011 = 3.
    if(FlagVar&1 == 1) //do something and then switch it off.
    {
    doSomethingFunc();
    (FlagVar) &= (~1);//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 00000001~=11111110. This turns off the 1st bit.
    }

    then for testing the second bit it would be like this;
    if(FlagVar&(1<<1)==(1<<1))
    {
    doSomenthingElseFunc();
    (FlagVar) &= (~(1<<1));
    }


    Hope this helps. It does take some practice...
    But once you get it you can really do some fast & fun stuff.

    Chris Reid

  6. #6
    Senior Member User Gallery
    Join Date
    Jun 2001
    Location
    Venezuela
    Age
    63
    Posts
    6,455

    Default

    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

  7. #7
    Moderator User Gallery
    Join Date
    Jun 2004
    Location
    UK
    Posts
    7,729

    Default

    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?

  8. #8
    Senior Member User Gallery
    Join Date
    Jul 2002
    Location
    Copenhagen
    Age
    39
    Posts
    2,464

    Default

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

    Code:
    [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"]]
    Mark

  9. #9
    Moderator User Gallery
    Join Date
    Jun 2004
    Location
    UK
    Posts
    7,729

    Default

    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!

  10. #10
    Senior Member User Gallery
    Join Date
    Jul 2002
    Location
    Copenhagen
    Age
    39
    Posts
    2,464

    Default

    Just testing to see if you were awake
    Mark

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •