Announcement

Collapse
No announcement yet.

What is the best way to translate <<

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • What is the best way to translate <<

    Hey All,

    I have this statement in C++ code, and im reaching way back here

    #define SOMECONSTANT (1<<4)

    this is a bit shift left but is it a macro?
    Sr. Software Development Engineer and Sr. Information Security Analyst,
    CEH, Digital Forensic Examiner

  • #2
    Code:
    %someconstant = 16&
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      so its not a macro but a constant?

      so, why even do

      #define SOMECONSTANT (1<<4)

      and not

      #define SOMECONSTANT 0x10

      ??
      Sr. Software Development Engineer and Sr. Information Security Analyst,
      CEH, Digital Forensic Examiner

      Comment


      • #4
        Sometimes you might define a constant with a calc like that to give a clue as to its origin. It could eg. be part of a set of like-calculated constants.

        Comment


        • #5
          I think there is someone around here always harping that he considers verb-for-verb, function-for-function, statement-for-statement "ports" from one source language to another "stone losers."

          I'll have to find that guy and send him an "Amen!" note with a link to this thread. I think a full text search on "stone loser" here ought to find him....

          MCM
          Michael Mattias
          Tal Systems (retired)
          Port Washington WI USA
          [email protected]
          http://www.talsystems.com

          Comment


          • #6
            > define a constant.. to give a clue as to its origin.

            In that case a more "self-documenting" definition might be..
            Code:
            %someconstant =  &b00010000
            MCM
            Michael Mattias
            Tal Systems (retired)
            Port Washington WI USA
            [email protected]
            http://www.talsystems.com

            Comment


            • #7
              From Seth Carnegie...

              "<<" is the left shift operator. It is shifting the number 1 to the left 0 bits, which is equivalent to the number 1.

              It is commonly used to create flags, numbers that can be combined together with | (bit or) and various operations can be applied to them, such as testing whether a flag is set, setting a flag, removing a flag, etc.

              The reason that they can be combined together without interfering with each other is that each one is a power of two, and that is the reason for using 1 << x, because that yields powers of two:

              1 << 0 == 20 == 1 == binary 0001
              1 << 1 == 21 == 2 == binary 0010
              1 << 2 == 22 == 4 == binary 0100
              1 << 3 == 23 == 8 == binary 1000
              etc
              Pierre

              Comment


              • #8
                Originally posted by Thomas Tierney View Post
                so its not a macro but a constant?

                so, why even do
                ...
                They could easily have coded it explicitly with a comment like so:

                #define SOMECONSTANT 16 ' Bit 4

                Presumably, this constant is used to mask off bit 4 in a variable of bit flags. With PB's BIT statements/functions, I prefer to define constants like so (for this purpose):

                %FlagDescription = 4 'Bit 4 of <some> flag variable

                IF BIT( <flag variable>, %FlagDescription) THEN ...
                Bernard Ertl
                InterPlan Systems

                Comment


                • #9
                  >They could easily have coded it explicitly with a comment like so:

                  'c' supports comments right in the source code? Really?

                  Unless you had told me I'd never have known from all the 'c' code I've seen posted.

                  I also have come to believe all 'c' compilers offer an '#OBFUSCATE ON|OFF' directive.. which defaults to ON.


                  MCM
                  Michael Mattias
                  Tal Systems (retired)
                  Port Washington WI USA
                  [email protected]
                  http://www.talsystems.com

                  Comment


                  • #10
                    nm
                    Last edited by Bern Ertl; 10 May 2013, 03:00 PM.
                    Bernard Ertl
                    InterPlan Systems

                    Comment


                    • #11
                      Originally posted by Michael Mattias View Post
                      >They could easily have coded it explicitly with a comment like so:

                      'c' supports comments right in the source code? Really?

                      Unless you had told me I'd never have known from all the 'c' code I've seen posted.

                      I also have come to believe all 'c' compilers offer an '#OBFUSCATE ON|OFF' directive.. which defaults to ON.


                      MCM
                      Haven't you heard? It's the artist, not the brush.

                      I've seen C code that is pure elegance and artistry.
                      I've also seen some really shoddy unreadable PB code.

                      Comment


                      • #12
                        Originally posted by Michael Mattias View Post
                        >
                        I also have come to believe all 'c' compilers offer an '#OBFUSCATE ON|OFF' directive.. which defaults to ON.
                        It's an editor directive, actually. The compiler doesn't care.
                        The editor must be instructed at every step of the code construction.
                        One must consciously include obfuscation in each instruction.
                        But just trying to be clever is sufficient, so it's not as hard as it seems.
                        The world is strange and wonderful.*
                        I reserve the right to be horrifically wrong.
                        Please maintain a safe following distance.
                        *wonderful sold separately.

                        Comment

                        Working...
                        X