Expert advice needed

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Smith

    Expert advice needed

    Hi All,

    Sorry if I am calling expert's advice on such a simple thing. I am reading
    a c++ code but do not understand what this means?

    #define DM_ITEMS 6
    #define FM_ITEMS ( 1 << (DM_ITEMS - 1) )

    when I look at the value of FM_ITEMS in my debug log it is 32.

    How in the heven it comes up with that number?

    It will be really nice to get a handle on this puzzle.

    Thanks you in advance.

    J.


  • E. Robert Tisdale

    #2
    Re: Expert advice needed

    John Smith wrote:
    [color=blue]
    >
    > Sorry if I am calling expert's advice on such a simple thing.
    > I am reading a C++ code but do not understand what this means?
    >
    > #define DM_ITEMS 6
    > #define FM_ITEMS ( 1 << (DM_ITEMS - 1) )
    >
    > when I look at the value of FM_ITEMS in my debug log it is 32.
    >
    > How in the heaven it comes up with that number?[/color]

    (1 << (DM_ITEMS - 1)) = (1 << (6 - 1)) = (1 << 5) = 1*pow(2, 5) = 1*32

    Comment

    • Quixote

      #3
      Re: Expert advice needed

      "John Smith" <masoudsafi@hot mail.com> wrote in message
      news:bhjsjt$2fj r$1@madmax.keyw ay.net[color=blue]
      > Hi All,
      >
      > Sorry if I am calling expert's advice on such a simple thing. I am
      > reading a c++ code but do not understand what this means?
      >
      > #define DM_ITEMS 6
      > #define FM_ITEMS ( 1 << (DM_ITEMS - 1) )
      >
      > when I look at the value of FM_ITEMS in my debug log it is 32.
      >
      > How in the heven it comes up with that number?
      >
      > It will be really nice to get a handle on this puzzle.
      >
      > Thanks you in advance.
      >
      > J.[/color]

      << is the left shift operator. x<<n shifts all digits in the number x to the
      left by n places. In this case, it shifts all digits in 1 to the left by 5
      places. Thus you start with

      000000000000000 000000000000000 01

      and end up with

      000000000000000 000000000001000 00

      i.e., ignoring the leading zeros, you go from

      1

      to

      100000

      100000 in binary is 32 in decimal (it is 2 raised to the power of 5).


      --

      John Carson
      1. To reply to email address, remove donald
      2. Don't reply to email address (post here instead)


      Comment

      • John Carson

        #4
        Re: Expert advice needed

        "Matt" <bigmw@charter. net> wrote in message
        news:vjra1b1gjs 284b@corp.super news.com[color=blue]
        > "Quixote" <donaldquixote@ datafast.net.au > wrote in message
        > news:3f3d92cd_2 @news.brisbane. pipenetworks.co m...[color=green]
        > >
        > > << is the left shift operator. x<<n shifts all digits in the number
        > > x to the left by n places. In this case, it shifts all digits in 1
        > > to the left by 5[/color]
        >
        > I believe that here you meant bits, not digits.
        > Digits is a generic term that can apply to any base, such as decimal
        > or binary (that's why bits = binary digits).
        >[/color]

        Yes. I meant binary digits == bits. Thanks for clarifying that.


        --

        John Carson
        1. To reply to email address, remove donald
        2. Don't reply to email address (post here instead)


        Comment

        • John Smith

          #5
          Re: Expert advice needed

          Hi Matt.

          I am writting an OCX control, and a vb app for a computer telephony
          application. I need to map the hardware return values which are defined in
          the c++ header file, to constants in my VB app. I managed to cover most of
          them but this one got me puzzled.

          Here are a few lines from my .H file:

          #define DE_RINGS 1 /* Rings received */
          #define DE_SILON 2 /* Silence on */
          #define DE_SILOF 3 /* Silenec off */
          ..
          ..
          ..
          #define DM_RINGS ( 1 << (DE_RINGS - 1) )
          #define DM_SILON ( 1 << (DE_SILON - 1) )
          #define DM_SILOF ( 1 << (DE_SILOF - 1) )

          In this ( << ) shift operation I do not see how it could rais any number by
          any power. However, I setup some logging and this is what I got:

          DE_SILOF=3, DM_SILOF=4
          DE_LCOF=5, DM_LCOF=16
          DE_WINK=6, DM_WINK=32
          DE_DIGOFF=9, DM_DIGOFF=256

          You are right it is raising 2 by the power of DE_XXXX values, but I just
          don't see how.

          I really appreciate your time.

          John.


          "Matt" <bigmw@charter. net> wrote in message
          news:vjra1b1gjs 284b@corp.super news.com...[color=blue]
          >
          > "Quixote" <donaldquixote@ datafast.net.au > wrote in message
          > news:3f3d92cd_2 @news.brisbane. pipenetworks.co m...[color=green]
          > > "John Smith" <masoudsafi@hot mail.com> wrote in message
          > > news:bhjsjt$2fj r$1@madmax.keyw ay.net[color=darkred]
          > > > Hi All,
          > > >
          > > > Sorry if I am calling expert's advice on such a simple thing. I am
          > > > reading a c++ code but do not understand what this means?
          > > >
          > > > #define DM_ITEMS 6
          > > > #define FM_ITEMS ( 1 << (DM_ITEMS - 1) )
          > > >
          > > > when I look at the value of FM_ITEMS in my debug log it is 32.
          > > >
          > > > How in the heven it comes up with that number?
          > > >
          > > > It will be really nice to get a handle on this puzzle.
          > > >
          > > > Thanks you in advance.
          > > >
          > > > J.[/color]
          > >
          > > << is the left shift operator. x<<n shifts all digits in the number x to[/color]
          > the[color=green]
          > > left by n places. In this case, it shifts all digits in 1 to the left by[/color][/color]
          5[color=blue]
          >
          > I believe that here you meant bits, not digits.
          > Digits is a generic term that can apply to any base, such as decimal or
          > binary (that's why bits = binary digits).
          >[color=green]
          > > places. Thus you start with
          > >
          > > 000000000000000 000000000000000 01
          > >
          > > and end up with
          > >
          > > 000000000000000 000000000001000 00
          > >
          > > i.e., ignoring the leading zeros, you go from
          > >
          > > 1
          > >
          > > to
          > >
          > > 100000
          > >
          > > 100000 in binary is 32 in decimal (it is 2 raised to the power of 5).[/color]
          >
          > To elaborate on the concept of bit shifts for John, this process is[/color]
          usually[color=blue]
          > carried out with imaging, encryption, bit masks (a very efficient form of
          > setting boolean values using individual bits), and steganography (hiding
          > messages in pictures/packets/music or other mediums), among others.
          > It can also be used to make more obfuscated as well, but i don't think
          > that's the case w/ your code.
          >
          > Just curious, what is this "c++ code" that you're reading supposed to do?[color=green]
          > >[/color]
          >
          > Just clarifying,
          >
          > Matt
          >
          >[/color]


          Comment

          • netnews.comcast.net

            #6
            Re: Expert advice needed

            Hi
            "John Smith" <masoudsafi@hot mail.com> wrote in message
            news:bhlti8$ge1 $1@madmax.keywa y.net...[color=blue]
            >
            > In this ( << ) shift operation I do not see how it could rais any number[/color]
            by[color=blue]
            > any power. However, I setup some logging and this is what I got:
            >
            > DE_SILOF=3, DM_SILOF=4
            > DE_LCOF=5, DM_LCOF=16
            > DE_WINK=6, DM_WINK=32
            > DE_DIGOFF=9, DM_DIGOFF=256
            >
            > You are right it is raising 2 by the power of DE_XXXX values, but I just
            > don't see how.[/color]

            Matt has explained it very nicely below. If you still have trouble, get a
            book
            on binary arithmetic.


            [color=blue]
            > "Matt" <bigmw@charter. net> wrote in message
            > news:vjra1b1gjs 284b@corp.super news.com...[color=green]
            > >
            > > "Quixote" <donaldquixote@ datafast.net.au > wrote in message
            > > news:3f3d92cd_2 @news.brisbane. pipenetworks.co m...[color=darkred]
            > > > "John Smith" <masoudsafi@hot mail.com> wrote in message
            > > > news:bhjsjt$2fj r$1@madmax.keyw ay.net
            > > > > Hi All,
            > > > >
            > > > > Sorry if I am calling expert's advice on such a simple thing. I am
            > > > > reading a c++ code but do not understand what this means?
            > > > >
            > > > > #define DM_ITEMS 6
            > > > > #define FM_ITEMS ( 1 << (DM_ITEMS - 1) )
            > > > >
            > > > > when I look at the value of FM_ITEMS in my debug log it is 32.
            > > > >
            > > > > How in the heven it comes up with that number?
            > > > >
            > > > > It will be really nice to get a handle on this puzzle.
            > > > >
            > > > > Thanks you in advance.
            > > > >
            > > > > J.
            > > >
            > > > << is the left shift operator. x<<n shifts all digits in the number x[/color][/color][/color]
            to[color=blue][color=green]
            > > the[color=darkred]
            > > > left by n places. In this case, it shifts all digits in 1 to the left[/color][/color][/color]
            by[color=blue]
            > 5[color=green]
            > >
            > > I believe that here you meant bits, not digits.
            > > Digits is a generic term that can apply to any base, such as decimal or
            > > binary (that's why bits = binary digits).
            > >[color=darkred]
            > > > places. Thus you start with
            > > >
            > > > 000000000000000 000000000000000 01
            > > >
            > > > and end up with
            > > >
            > > > 000000000000000 000000000001000 00
            > > >
            > > > i.e., ignoring the leading zeros, you go from
            > > >
            > > > 1
            > > >
            > > > to
            > > >
            > > > 100000
            > > >
            > > > 100000 in binary is 32 in decimal (it is 2 raised to the power of 5).[/color]
            > >
            > > To elaborate on the concept of bit shifts for John, this process is[/color]
            > usually[color=green]
            > > carried out with imaging, encryption, bit masks (a very efficient form[/color][/color]
            of[color=blue][color=green]
            > > setting boolean values using individual bits), and steganography (hiding
            > > messages in pictures/packets/music or other mediums), among others.
            > > It can also be used to make more obfuscated as well, but i don't think
            > > that's the case w/ your code.
            > >
            > > Just curious, what is this "c++ code" that you're reading supposed to[/color][/color]
            do?[color=blue][color=green][color=darkred]
            > > >[/color]
            > >
            > > Just clarifying,
            > >
            > > Matt
            > >
            > >[/color]
            >
            >[/color]


            Comment

            • Matt

              #7
              Re: Expert advice needed


              "John Smith" <masoudsafi@hot mail.com> wrote in message
              news:bhlti8$ge1 $1@madmax.keywa y.net...[color=blue]
              > Hi Matt.
              >
              > I am writting an OCX control, and a vb app for a computer telephony
              > application. I need to map the hardware return values which are defined[/color]
              in[color=blue]
              > the c++ header file, to constants in my VB app. I managed to cover most[/color]
              of[color=blue]
              > them but this one got me puzzled.
              >
              > Here are a few lines from my .H file:
              >
              > #define DE_RINGS 1 /* Rings received */
              > #define DE_SILON 2 /* Silence on */
              > #define DE_SILOF 3 /* Silenec off */
              > .
              > .
              > .
              > #define DM_RINGS ( 1 << (DE_RINGS - 1) )
              > #define DM_SILON ( 1 << (DE_SILON - 1) )
              > #define DM_SILOF ( 1 << (DE_SILOF - 1) )
              >
              > In this ( << ) shift operation I do not see how it could rais any number[/color]
              by[color=blue]
              > any power. However, I setup some logging and this is what I got:
              >
              > DE_SILOF=3, DM_SILOF=4
              > DE_LCOF=5, DM_LCOF=16
              > DE_WINK=6, DM_WINK=32
              > DE_DIGOFF=9, DM_DIGOFF=256
              >
              > You are right it is raising 2 by the power of DE_XXXX values, but I just
              > don't see how.[/color]

              Ok, I guess I'll explain how to compute binary to you.
              When numbers are represented in a computer, they are stored in binary
              (zeroes and ones, you've prob heard this in some intro to computers
              course/book/site or something). Binary is only different from decimal in
              the base that it represents numbers. Decimal is base-10, binary is base-2
              (bi = 2). Each "digit" (bit to be exact) in binary represents a power of 2.
              Just like each digit in decimal represents a power of 10. For example:

              decimal = 32 = (3 * 10^1) + (2 * 10^0)
              binary = 100000 = (1 * 2^5) + (0 * 2^4) + (0 * 2^3) + (0 * 2^2) + (0 * 2^1)
              + (0 * 2^0)
              Both are equivalent to 32 in decimal.
              Soooo, in binary, starting from the rightmost bit, each bit represents a
              certain power of the base (starting w/ 0 which anything to the 0 power is
              1). With binary this might be more help:

              2^5 2^4 2^3 2^2 2^1 2^0
              1 0 0 0 0 0

              This is why 100000 is 32.
              Now to apply this to your bitshift ( << ), what this operator does is take
              whatever number is on the left side and shifts all the bits in that number
              the number of places denoted by the number on the right side (filling in
              empty spaces to the right with 0's). Example:

              #define DE_RINGS 1
              #define DE_SILON 2
              #define DE_SILOF 3

              this is the way that the values are represented in the computer
              DE_RINGS = 1 = (1 * 2^0) = 1 in decimal
              DE_SILON = 10 = (1 * 2^1) + (0 * 2^0) = 2
              DE_SILOF = 11 = (1 * 2^1) + (1 * 2^0) = 3

              So if you say

              (1 << (DE_SILOF - 1))

              (DE_SILOF - 1) == (3 - 1) == 2

              (1 << 2) == 1 << 2 == take the binary representation of 1 and shift all the
              bits two places to the left and fill in new places w/ 0's.

              ie: 0001 (leading 0's just so that we know it's binary)
              becomes 0100 or

              (0 * 2^3) + (1 * 2^2) + (0 * 2^1) + (0 * 2^0)

              that's why things are "raising 2 by the power of DE_XXXX values", because
              the shift operation is just moving a 1 to a certain power of 2 in binary.

              Hope that further clarifies all of our answers.
              If you still have questions, you can do a google search for more detailed
              info on these topics
              [color=blue]
              >
              > I really appreciate your time.
              >[/color]

              Your Welcome,
              Matt
              [color=blue]
              > John.
              >
              >
              > "Matt" <bigmw@charter. net> wrote in message
              > news:vjra1b1gjs 284b@corp.super news.com...[color=green]
              > >
              > > "Quixote" <donaldquixote@ datafast.net.au > wrote in message
              > > news:3f3d92cd_2 @news.brisbane. pipenetworks.co m...[color=darkred]
              > > > "John Smith" <masoudsafi@hot mail.com> wrote in message
              > > > news:bhjsjt$2fj r$1@madmax.keyw ay.net
              > > > > Hi All,
              > > > >
              > > > > Sorry if I am calling expert's advice on such a simple thing. I am
              > > > > reading a c++ code but do not understand what this means?
              > > > >
              > > > > #define DM_ITEMS 6
              > > > > #define FM_ITEMS ( 1 << (DM_ITEMS - 1) )
              > > > >
              > > > > when I look at the value of FM_ITEMS in my debug log it is 32.
              > > > >
              > > > > How in the heven it comes up with that number?
              > > > >
              > > > > It will be really nice to get a handle on this puzzle.
              > > > >
              > > > > Thanks you in advance.
              > > > >
              > > > > J.
              > > >
              > > > << is the left shift operator. x<<n shifts all digits in the number x[/color][/color][/color]
              to[color=blue][color=green]
              > > the[color=darkred]
              > > > left by n places. In this case, it shifts all digits in 1 to the left[/color][/color][/color]
              by[color=blue]
              > 5[color=green]
              > >
              > > I believe that here you meant bits, not digits.
              > > Digits is a generic term that can apply to any base, such as decimal or
              > > binary (that's why bits = binary digits).
              > >[color=darkred]
              > > > places. Thus you start with
              > > >
              > > > 000000000000000 000000000000000 01
              > > >
              > > > and end up with
              > > >
              > > > 000000000000000 000000000001000 00
              > > >
              > > > i.e., ignoring the leading zeros, you go from
              > > >
              > > > 1
              > > >
              > > > to
              > > >
              > > > 100000
              > > >
              > > > 100000 in binary is 32 in decimal (it is 2 raised to the power of 5).[/color]
              > >
              > > To elaborate on the concept of bit shifts for John, this process is[/color]
              > usually[color=green]
              > > carried out with imaging, encryption, bit masks (a very efficient form[/color][/color]
              of[color=blue][color=green]
              > > setting boolean values using individual bits), and steganography (hiding
              > > messages in pictures/packets/music or other mediums), among others.
              > > It can also be used to make more obfuscated as well, but i don't think
              > > that's the case w/ your code.
              > >
              > > Just curious, what is this "c++ code" that you're reading supposed to[/color][/color]
              do?[color=blue][color=green][color=darkred]
              > > >[/color]
              > >
              > > Just clarifying,
              > >
              > > Matt
              > >
              > >[/color]
              >
              >[/color]


              Comment

              • Ahti Legonkov

                #8
                Re: Expert advice needed

                John Smith wrote:
                [color=blue]
                > In this ( << ) shift operation I do not see how it could rais any number by
                > any power. However, I setup some logging and this is what I got:
                >
                > DE_SILOF=3, DM_SILOF=4
                > DE_LCOF=5, DM_LCOF=16
                > DE_WINK=6, DM_WINK=32
                > DE_DIGOFF=9, DM_DIGOFF=256
                >
                > You are right it is raising 2 by the power of DE_XXXX values, but I just
                > don't see how.[/color]

                a<<b doesn't raise any number to any power. It just multiplies a by
                pow(2, b). Consider this - if you append zero to a number, you have
                multiplied it by 10. If you append 2 zeroes, you have multiplied it by
                100. That's what shifting bits left does. And 10 and 100 are not decimal
                number but binary number (ie. 2 and 4).

                --
                Ahti Legonkov

                Comment

                Working...