Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: [ask] About set /a ??  (Read 4260 times)

0 Members and 1 Guest are viewing this topic.

Fen_Li

    Topic Starter


    Beginner

  • G-Smart thing Smart
  • Thanked: 2
    [ask] About set /a ??
    « on: September 30, 2007, 08:47:19 AM »
    i have read set command help (set /?) more time but i'am not enough understand about "set /a operator" :

    i.e. quote from set /?
    Quote
    Two new switches have been added to the SET command:

        SET /A expression
        SET /P variable=[promptString]

    The /A switch specifies that the string to the right of the equal sign
    is a numerical expression that is evaluated.  The expression evaluator
    is pretty simple and supports the following operations, in decreasing
    order of precedence:

        ()                  - grouping
        ! ~ -               - unary operators
        * / %               - arithmetic operators
        + -                 - arithmetic operators
        << >>               - logical shift
        &                   - bitwise and
        ^                   - bitwise exclusive or
        |                   - bitwise or
        = *= /= %= += -=    - assignment
          &= ^= |= <<= >>=
        ,                   - expression separator

    i not clearly about set /a operator like : |,^,&
    anybody can give an example for using bitwise or "|" and bitwise exclusive or "^" on set /a command  ???



    thanks..

    ..Still Newbie KID..

    contrex

    • Guest
    Re: [ask] About set /a ??
    « Reply #1 on: September 30, 2007, 11:40:51 AM »
    How can I convert a decimal number to binary, octal, hexadecimal?

    Here is a way to convert a 8-bit decimal to its binary equivalent.
    The formulation below emphasizes extracting each bit individually.

       @echo off & setlocal enableextensions enabledelayedexpansion
       set decimal_=%1
       set /a b0=%decimal_% ^& 1
       set /a b1=%decimal_% ^& 2
       set /a b2=%decimal_% ^& 4
       set /a b3=%decimal_% ^& 8
       set /a b4=%decimal_% ^& 16
       set /a b5=%decimal_% ^& 32
       set /a b6=%decimal_% ^& 64
       set /a b7=%decimal_% ^& 128
       set i=-1
       for %%b in (!b0! !b1! !b2! !b3! !b4! !b5! !b6! !b7!) do (
         set bit=%%b
         set /a i+=1
         if !bit! EQU 0 (echo bit !i! = 0) else (echo bit !i! = 1)
       )
       endlocal & goto :EOF


    An example of the output
     
       bit 0 = 0
       bit 1 = 0
       bit 2 = 1
       bit 3 = 1
       bit 4 = 0
       bit 5 = 0
       bit 6 = 0
       bit 7 = 0

    Fen_Li

      Topic Starter


      Beginner

    • G-Smart thing Smart
    • Thanked: 2
      Re: [ask] About set /a ??
      « Reply #2 on: October 01, 2007, 01:55:54 PM »
      thanks..it's great...
      nice formula..i think

      but, i am still not clearly, especially with "^&"
      can u give explain that more basicly...
      i.e. -->
      - set /a "2^3"          ; it return 1
      - set /a "3^4"          ; it return 7

      what is the meaning of "^" ??
      why 2^3=1 (3-2)....and 3^4=7 (3+4) ??


      ..Still Newbie KID..

      contrex

      • Guest
      Re: [ask] About set /a ??
      « Reply #3 on: October 01, 2007, 02:17:38 PM »
      Some confusion because the ^ ("caret") character has 2 purposes...

      (1) it is used to "escape" special characters in batch files, especially the & character, which breaks commands otherwise. For example to use a & b in a batch file you must type a ^& b. I know this is confusing but you just have to learn it if you need to do batch arithmentic!

      (2) In batch arithmetic the ^ character is also the "bitwise exclusive or" operator.

      A bitwise exclusive or takes two bit patterns of equal length and performs the logical XOR operation on each pair of corresponding bits. The result in each position is 1 if the two bits are different, and 0 if they are the same.

      You can perform it manually like for school arithmetic as I shall demonstrate:-

      Place an 0 in the answer if the bits are the same and a 1 if they are different

      Quote
      2 =      00000010
      3 =      00000011
               --------
      result = 00000001 = 1

      3 =      00000011
      4 =      00000100
               --------
      result = 00000111 = 7

      QED




             

      Fen_Li

        Topic Starter


        Beginner

      • G-Smart thing Smart
      • Thanked: 2
        Re: [ask] About set /a ??
        « Reply #4 on: October 01, 2007, 02:44:47 PM »
        hmmm...
        yea, like a logical arithmetic lesson in school ...(maybe in Junior High School)
        it's long time ago...


        littte mind about logical arithmetic
        but, it an OR "|" operator..not XOR

        [expression 1] or [expression 2] = Result
        if expression 1 is true and expression 2 true , result will be True
        if one of expression is False , result will be False

        ilustrate on table:
        Quote
        ----------------------
        A    B          A or B
        ----------------------
        T    T              T
        T    F              F
        F    T              F
        F    F              F
        note: T=True , F=False

        is this same think as your demonstration ??
        « Last Edit: October 01, 2007, 03:24:21 PM by Fen_Li »
        ..Still Newbie KID..

        contrex

        • Guest
        Re: [ask] About set /a ??
        « Reply #5 on: October 01, 2007, 03:34:57 PM »
        is this same think as your demonstration ??

        similar idea.

        Fen_Li

          Topic Starter


          Beginner

        • G-Smart thing Smart
        • Thanked: 2
          Re: [solved] About set /a ??
          « Reply #6 on: October 01, 2007, 03:57:13 PM »
          thanks so far for solved my problem...
          that's great help...

          maybe, I will do some trial an error....to enhance experience..
          coz, 'experience is the best teacher'

          Regard...
          ..Still Newbie KID..