Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Bit concatenation in C++

Bit concatenation in C++

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialc++help
21 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Manoj7390

    I am implementing turbo encoder, I will give some data as a input and i am converting it into the binary, since i don't know how to store and which data type for binary values, i have stored those binary values as integers only. Now i need to concatenate three bits at a time, I need only 1's and 0's not the integer values. If i shift these values i will get more than 1.

    CPalliniC Offline
    CPalliniC Offline
    CPallini
    wrote on last edited by
    #10

    Manoj7390 wrote:

    If i shift these values i will get more than 1

    Of course. Now look at decimal case: if you 'concatenate' the digits 1,5,7 then you get the number 157 that is no more in the 0-9 range. An alternative would be using strings, that is, you concatenate the character representations of the digits into a string ('1'', '5', '7' => "157"). You may apply exactly the same argument to binary (that is base 2) digits: if you concatenate 1,0,0 you get the number 101, that is decimal 5, no more in the 0-1 range. The string concatenation method would give you "101". You may find interesting the std::bitset[^] class. However it does not prevent you from gaining a proper understanding of numerical bases (see, for instance, Numeral System at Wikipedia[^]) and how numbers are stored in computers.

    Veni, vidi, vici.

    In testa che avete, signor di Ceprano?

    1 Reply Last reply
    0
    • T ThatsAlok

      if you looking for string value as i understand by double quote used by you, you can use sprintf or sprintf_s. now if you want to store the binary value resulting from you operation (as you mentioned they are only 3 bit coming), you can use char datatype (1 byte) and store the decimal equivalent of it there, internally it store its binary equivalent only, for say you got 101 which decimal equivalent is 5 and when you store same in char variable, it binary representation would be 00000100

      "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
      Never mind - my own stupidity is the source of every "problem" - Mixture

      cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You

      M Offline
      M Offline
      Manoj7390
      wrote on last edited by
      #11

      But i need to concatenate the bits which are in different variables. how to concatenate those.. Since i cannot use shift operator here because of binary.

      L 1 Reply Last reply
      0
      • T ThatsAlok

        if you looking for string value as i understand by double quote used by you, you can use sprintf or sprintf_s. now if you want to store the binary value resulting from you operation (as you mentioned they are only 3 bit coming), you can use char datatype (1 byte) and store the decimal equivalent of it there, internally it store its binary equivalent only, for say you got 101 which decimal equivalent is 5 and when you store same in char variable, it binary representation would be 00000100

        "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
        Never mind - my own stupidity is the source of every "problem" - Mixture

        cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You

        M Offline
        M Offline
        Manoj7390
        wrote on last edited by
        #12

        Can you please suggest me a program to convert a string into binary, that store in char data type.

        C 1 Reply Last reply
        0
        • L Lost User

          Manoj7390 wrote:

          Suggest any other way to store bits in C/C++

          This has nothing to do with C/C++, it's how computers work. If you will not use the shift operators to store the three bits together then you cannot store them.

          Use the best guess

          C Offline
          C Offline
          Chris Losinger
          wrote on last edited by
          #13

          Richard MacCutchan wrote:

          If you will not use the shift operators to store the three bits together then you cannot store them.

          int Answer = u * 4 + p * 2 + q

          image processing toolkits | batch image processing

          L 1 Reply Last reply
          0
          • M Manoj7390

            Can you please suggest me a program to convert a string into binary, that store in char data type.

            C Offline
            C Offline
            Chris Losinger
            wrote on last edited by
            #14

            char * p = "101";
            char * pEnd;
            int val = strtol(p, &pEnd, 2);
            char charval = (char)val;

            this may fail if p is more than 7 bits long (since that could overflow a signed char).

            image processing toolkits | batch image processing

            1 Reply Last reply
            0
            • C Chris Losinger

              Richard MacCutchan wrote:

              If you will not use the shift operators to store the three bits together then you cannot store them.

              int Answer = u * 4 + p * 2 + q

              image processing toolkits | batch image processing

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #15

              Which the compiler will almost certainly optimise into shift operations.

              Use the best guess

              C 1 Reply Last reply
              0
              • M Manoj7390

                But i need to concatenate the bits which are in different variables. how to concatenate those.. Since i cannot use shift operator here because of binary.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #16

                Manoj7390 wrote:

                i cannot use shift operator here because of binary.

                What on earth are you talking about? I have shown you exactly how to concatenate these fields into a three bit value. If you really cannot understand that simple operation then I don't think you are going to get very far with this project.

                Use the best guess

                1 Reply Last reply
                0
                • L Lost User

                  Which the compiler will almost certainly optimise into shift operations.

                  Use the best guess

                  C Offline
                  C Offline
                  Chris Losinger
                  wrote on last edited by
                  #17

                  possibly. but that extends the definition of "you" quite a bit. int Answer = u + u + u + u + p + p + q;

                  image processing toolkits | batch image processing

                  L 1 Reply Last reply
                  0
                  • M Manoj7390

                    Hi. I have written the program for turbo encoder, for every single bit, it gives three bits as output and I am storing it as a integer. Now the problem is how to concatenate these bits in the integer form. for example... int u = 1; int p = 0; int q = 1; Answer = 101;

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #18

                    Something like:

                    int answer = (u*100) + (p*10) + q;

                    "One man's wage rise is another man's price increase." - Harold Wilson

                    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                    "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

                    1 Reply Last reply
                    0
                    • C Chris Losinger

                      possibly. but that extends the definition of "you" quite a bit. int Answer = u + u + u + u + p + p + q;

                      image processing toolkits | batch image processing

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #19

                      Chris Losinger wrote:

                      that extends the definition of "you" quite a bit.

                      :confused:

                      Use the best guess

                      C 1 Reply Last reply
                      0
                      • L Lost User

                        Chris Losinger wrote:

                        that extends the definition of "you" quite a bit.

                        :confused:

                        Use the best guess

                        C Offline
                        C Offline
                        Chris Losinger
                        wrote on last edited by
                        #20

                        you originally wrote "If you will not use the shift operators to store the three bits together then you cannot store them." that's the "you". i think it's reasonable to differentiate between what the programmer writes and what the compiler does.

                        image processing toolkits | batch image processing

                        L 1 Reply Last reply
                        0
                        • C Chris Losinger

                          you originally wrote "If you will not use the shift operators to store the three bits together then you cannot store them." that's the "you". i think it's reasonable to differentiate between what the programmer writes and what the compiler does.

                          image processing toolkits | batch image processing

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #21

                          Sorry, but I don't understand what point you are trying to make.

                          Use the best guess

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups