Bitwise operators

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

    #1

    Bitwise operators

    Hi to all,
    How the individual digits of a number can be obtained using the
    bitwise operators alone.Is it possible to do it ?

    If we have n = 34

    Result has to be 3,4.

    Thanks a billion for your reply in advance.
  • Walter Roberson

    #2
    Re: Bitwise operators

    In article <39f5b255-0336-4f6f-84b5-f0013d30d2c4@p3 9g2000prm.googl egroups.com>,
    Santhosh <santhoshvenkat 1988@gmail.comw rote:
    >How the individual digits of a number can be obtained using the
    >bitwise operators alone.Is it possible to do it ?
    If we have n = 34
    >Result has to be 3,4.
    This is obviously an artificial question such as for a homework
    assignment or an interview question, so I decline to demonstrate.

    I would recommend, though, being more explicit as to which
    are the "bitwise operators" that are to be considered. For example
    would a solution be ruled out if it had loops or conditional
    tests? Is comparision to 0 a bitwise operation ?
    --
    "There is no greater calling than to serve your fellow men.
    There is no greater contribution than to help the weak.
    There is no greater satisfaction than to have done it well."
    -- Walter Reuther

    Comment

    • =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?=

      #3
      Re: Bitwise operators

      On Jun 18, 10:32 pm, Santhosh <santhoshvenkat 1...@gmail.comw rote:
       If we have n  = 34
      >
      Result has to be 3,4

      Ever heard of mathematics? Number systems?

      A typical number system consists of:
      1) Radix = the amount of different symbols
      2) The actual pictures that represent the symbols

      Let's take the "octal" number system:
      1) Radix = 8
      2) Symbols = 0 1 2 3 4 5 6 7

      If you want to represent a number that is greater than or equal to
      radix, then you need more than one digit. So in octal, the number
      eight is written as:
      10
      And nine is written as:
      11

      Here, the "11" is equal to:
      1 multiplied by (8 to the power of 1)
      +1 multiplied by (8 to the power of 0)

      Try another octal number: 2763
      2 multiplied by (8 to the power of 3)
      +7 multiplied by (8 to the power of 2)
      +6 multiplied by (8 to the power of 1)
      +3 multiplied by (8 to the power of 0)

      Try think now, if you had a number in decimal such as 34, then what
      would you do to it to get the first digit and the second digit? I'll
      give you a clue, it involves using the radix, i.e. 10, in conjunction
      with a mathematical operation such as division.

      Comment

      • Peter Nilsson

        #4
        Re: Bitwise operators

        Santhosh wrote:
        Hi to all,
        How the individual digits of a number can be obtained using the
        bitwise operators alone.Is it possible to do it ?
        Yes, but that isn't a question about C, merely a question on
        algorithms. Google for BCD.

        --
        Peter

        Comment

        • Walter Roberson

          #5
          Re: Bitwise operators

          In article <01badd00-6bb6-4ccc-abd0-4126a5d156a1@w3 4g2000prm.googl egroups.com>,
          Peter Nilsson <airia@acay.com .auwrote:
          >Santhosh wrote:
          >How the individual digits of a number can be obtained using the
          >bitwise operators alone.Is it possible to do it ?
          >Yes, but that isn't a question about C, merely a question on
          >algorithms. Google for BCD.
          In a way, it is a question about C, as "bitwise operators" would
          have to be interpreted in the context of C: the exact operators
          which are considered "bitwise" operators will make a difference
          to whether such an algorithm can be constructed.

          Turing equivilence only works for "sufficient ly powerful"
          computation systems, and the four operators ~ ^ & | alone
          are not "sufficient ly powerful" for to be able to generate
          any arbitrary algorithm.
          --
          "History is a pile of debris" -- Laurie Anderson

          Comment

          • Antoninus Twink

            #6
            Re: Bitwise operators

            On 18 Jun 2008 at 22:09, Walter Roberson wrote:
            Turing equivilence only works for "sufficient ly powerful" computation
            systems, and the four operators ~ ^ & | alone are not "sufficient ly
            powerful" for to be able to generate any arbitrary algorithm.
            They are sufficiently powerful to generate all the operations performed
            by your favorite ALU.

            Comment

            • Richard Heathfield

              #7
              Re: Bitwise operators

              Walter Roberson said:

              <snip>
              Turing equivilence only works for "sufficient ly powerful"
              computation systems, and the four operators ~ ^ & | alone
              are not "sufficient ly powerful" for to be able to generate
              any arbitrary algorithm.
              Well, I agree that you need a "jump operator" of some kind, and a "test
              operator". But it's amazing what you /can/ do with the ~ ^ & | ><<
              collection (you missed the shifts, which is presumably just an oversight).
              ^ gives you a half-adder. & and << give you the other half, handing you
              full addition on a plate. Using two's complement gives you subtraction
              almost for free. With those, you can do multiplication, division, and mod.
              And with those five arithmetic operators in place, you can do just about
              anything that can be done.

              --
              Richard Heathfield <http://www.cpax.org.uk >
              Email: -http://www. +rjh@
              Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
              "Usenet is a strange place" - dmr 29 July 1999

              Comment

              • Johannes Bauer

                #8
                Re: Bitwise operators

                Richard Heathfield schrieb:
                Well, I agree that you need a "jump operator" of some kind, and a "test
                operator".
                Not for this problem. The data type which is input has a finite size,
                therefore enumeration is possible, yielding an extremely huge and
                complex boolean expression that does not need one single jump or test.
                Probably far too huge to be compiled by any compiler, but, hey...

                Regards,
                Johannes

                --
                "Wer etwas kritisiert muss es noch lange nicht selber besser können. Es
                reicht zu wissen, daß andere es besser können und andere es auch
                besser machen um einen Vergleich zu bringen." - Wolfgang Gerber
                in de.sci.electron ics <47fa8447$0$115 45$9b622d9e@new s.freenet.de>

                Comment

                • Johannes Bauer

                  #9
                  Re: Bitwise operators

                  Johannes Bauer schrieb:
                  Richard Heathfield schrieb:
                  >
                  >Well, I agree that you need a "jump operator" of some kind, and a
                  >"test operator".
                  >
                  Not for this problem. The data type which is input has a finite size,
                  therefore enumeration is possible, yielding an extremely huge and
                  complex boolean expression that does not need one single jump or test.
                  Probably far too huge to be compiled by any compiler, but, hey...
                  For demonstration purposes, I've created a program which enumerates the
                  numbers from 0-99 and always does output the 10^1 digit:



                  It's mighty impressive ;-)

                  Regards,
                  Johannes

                  --
                  "Wer etwas kritisiert muss es noch lange nicht selber besser können. Es
                  reicht zu wissen, daß andere es besser können und andere es auch
                  besser machen um einen Vergleich zu bringen." - Wolfgang Gerber
                  in de.sci.electron ics <47fa8447$0$115 45$9b622d9e@new s.freenet.de>

                  Comment

                  • Richard Heathfield

                    #10
                    Re: Bitwise operators

                    Johannes Bauer said:
                    Richard Heathfield schrieb:
                    >
                    >Well, I agree that you need a "jump operator" of some kind, and a "test
                    >operator".
                    >
                    Not for this problem.
                    Quite. My comment was made with regard to Walter's more general claim that
                    "the four operators ~ ^ & | alone are not "sufficient ly powerful" for to
                    be able to generate any arbitrary algorithm".

                    --
                    Richard Heathfield <http://www.cpax.org.uk >
                    Email: -http://www. +rjh@
                    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                    "Usenet is a strange place" - dmr 29 July 1999

                    Comment

                    • Walter Roberson

                      #11
                      Re: Bitwise operators

                      In article <n9poi5xkd5.ln2 @joeserver.home lan.net>,
                      Johannes Bauer <dfnsonfsduifb@ gmx.dewrote:
                      >For demonstration purposes, I've created a program which enumerates the
                      >numbers from 0-99 and always does output the 10^1 digit:
                      (using bitwise operators)
                      >It's mighty impressive ;-)
                      Definitely a labour of love!
                      --
                      "When a scientist is ahead of his times, it is often through
                      misunderstandin g of current, rather than intuition of future truth.
                      In science there is never any error so gross that it won't one day,
                      from some perspective, appear prophetic." -- Jean Rostand

                      Comment

                      • onkar.n.mahajan@gmail.com

                        #12
                        Re: Bitwise operators

                        On Jun 19, 2:32 am, Santhosh <santhoshvenkat 1...@gmail.comw rote:
                        Hi to all,
                        How the individual digits of a number can be obtained using the
                        bitwise operators alone.Is it possible to do it ?
                        >
                        If we have n = 34
                        >
                        Result has to be 3,4.
                        >
                        Thanks a billion for your reply in advance.
                        Indians always ask interview Questions ! :-)

                        Comment

                        • onkar.n.mahajan@gmail.com

                          #13
                          Re: Bitwise operators

                          On Jun 19, 2:32 am, Santhosh <santhoshvenkat 1...@gmail.comw rote:
                          Hi to all,
                          How the individual digits of a number can be obtained using the
                          bitwise operators alone.Is it possible to do it ?
                          >
                          If we have n = 34
                          >
                          Result has to be 3,4.
                          >
                          Thanks a billion for your reply in advance.
                          This is a comp.lang.c forum - "not to discuss interview Qs related to
                          algos here" , we are here to discuss Qs related to C programming
                          language !

                          Comment

                          • santosh

                            #14
                            Re: Bitwise operators

                            onkar.n.mahajan @gmail.com wrote:
                            On Jun 19, 2:32 am, Santhosh <santhoshvenkat 1...@gmail.comw rote:
                            >Hi to all,
                            >How the individual digits of a number can be obtained using the
                            >bitwise operators alone.Is it possible to do it ?
                            >>
                            > If we have n = 34
                            >>
                            >Result has to be 3,4.
                            >>
                            >Thanks a billion for your reply in advance.
                            >
                            This is a comp.lang.c forum - "not to discuss interview Qs related to
                            algos here" , we are here to discuss Qs related to C programming
                            language !
                            No, the question is perfectly legitimate[1]. That it may be
                            an "interview question" or a homework question is irrelevant. Of course
                            questions of this type often don't elicit the type of response that the
                            OP would like, but that's a different matter.

                            1. Always assuming that the OP wants a C based answer. Otherwise it's
                            not topical here and he should probably post to comp.programmin g.

                            Comment

                            • rahul

                              #15
                              Re: Bitwise operators

                              On Jun 19, 2:32 am, Santhosh <santhoshvenkat 1...@gmail.comw rote:
                              Hi to all,
                              How the individual digits of a number can be obtained using the
                              bitwise operators alone.Is it possible to do it ?
                              >
                              If we have n = 34
                              >
                              Result has to be 3,4.
                              >
                              Thanks a billion for your reply in advance.
                              We generally use % and / to get the individual digits.
                              #include <stdio.h>
                              #include <stdlib.h>

                              int
                              main(void) {
                              int num = 34;
                              int digit = 0;
                              while (num != 0) {
                              digit = num % 10;
                              num /= 10;
                              printf("%d\n", digit);
                              }
                              return 0;
                              }

                              The result will be 4, 3 as it starts separating from the unit's place.
                              You are not clear on where do you want to use the bitwise operator.

                              Comment

                              Working...