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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How do I set the first 10 BITS of 4 chars string to a letter??

How do I set the first 10 BITS of 4 chars string to a letter??

Scheduled Pinned Locked Moved C / C++ / MFC
question
7 Posts 5 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.
  • I Offline
    I Offline
    IrishSonic
    wrote on last edited by
    #1

    I have done this: struct s_Person{ char Letter1 : 10, Letter2 : 10, Letter3 : 12; }Person[5]; What I want to do is have a string of 4 chars ( which is 32 bits ), and set the first 10 BITS of the string to be letter 'a' or something else... The struct fails because it says that the field types are too small. How do I put a character(or characters) into the first 10 bits of a string of 4 lenght?? Also, if the data field is only 10 bits, which is 2 chars + 2 bits, the maximum I could actually fit into Letter1 would be 2 letters..Is this right?? Thanks, grahamoj.

    N M A J 4 Replies Last reply
    0
    • I IrishSonic

      I have done this: struct s_Person{ char Letter1 : 10, Letter2 : 10, Letter3 : 12; }Person[5]; What I want to do is have a string of 4 chars ( which is 32 bits ), and set the first 10 BITS of the string to be letter 'a' or something else... The struct fails because it says that the field types are too small. How do I put a character(or characters) into the first 10 bits of a string of 4 lenght?? Also, if the data field is only 10 bits, which is 2 chars + 2 bits, the maximum I could actually fit into Letter1 would be 2 letters..Is this right?? Thanks, grahamoj.

      N Offline
      N Offline
      Nish Nishant
      wrote on last edited by
      #2

      A character is 8 bits for one thing. And you cannot put an 'A' into a bit. You can only put a 1 or a 0 (turn it on or off basically). Nish


      Author of the romantic comedy Summer Love and Some more Cricket [New Win] Review by Shog9 Click here for review[NW]

      1 Reply Last reply
      0
      • I IrishSonic

        I have done this: struct s_Person{ char Letter1 : 10, Letter2 : 10, Letter3 : 12; }Person[5]; What I want to do is have a string of 4 chars ( which is 32 bits ), and set the first 10 BITS of the string to be letter 'a' or something else... The struct fails because it says that the field types are too small. How do I put a character(or characters) into the first 10 bits of a string of 4 lenght?? Also, if the data field is only 10 bits, which is 2 chars + 2 bits, the maximum I could actually fit into Letter1 would be 2 letters..Is this right?? Thanks, grahamoj.

        M Offline
        M Offline
        moliate
        wrote on last edited by
        #3

        As stated above a char is 8 bytes. You are trying to use 10 out of 8 bits. Below is an alternative (actually 3-in-one):

        struct s_Person
        {
        union
        {
        char A[4];

        	struct
        	{
        	char a;
        	char b;
        	char c;
        	char d;
        	};
        
        	unsigned int name;
        };
        

        };

        If you want to set the string to "ABCD", you could use:

        s_Person p;
        p.a = 'A';
        p.b = 'B';
        p.c = 'C';
        p.d = 'D';
        // or ...
        p.A[0] = 'A';
        p.A[1] = 'B';
        p.A[2] = 'C';
        p.A[3] = 'D';
        // or ...
        p.name = 'DCBA';

        The last one might look odd, but the byte order is because Intel has a little-endian architecture. Anyway, the total size of the structure is 32 bits. /moliate


        The corners of my eyes catch hasty, bloodless motion - a mouse? Well, certainly a peripheral of some kind.

        Neil Gaiman - Cold Colours

        1 Reply Last reply
        0
        • I IrishSonic

          I have done this: struct s_Person{ char Letter1 : 10, Letter2 : 10, Letter3 : 12; }Person[5]; What I want to do is have a string of 4 chars ( which is 32 bits ), and set the first 10 BITS of the string to be letter 'a' or something else... The struct fails because it says that the field types are too small. How do I put a character(or characters) into the first 10 bits of a string of 4 lenght?? Also, if the data field is only 10 bits, which is 2 chars + 2 bits, the maximum I could actually fit into Letter1 would be 2 letters..Is this right?? Thanks, grahamoj.

          A Offline
          A Offline
          Abbas_Riazi
          wrote on last edited by
          #4

          As you know int takes 4 bytes in memory (in Win32), we can use union to do it for us.

          union _Person
          {
          char Letters[4];
          int dummy;
          } Person[5];

          OK, now you want to assign 10 bits to your Letters (for example 11 0101 0101 or 0x355). Person[0].dummy=0x355; after using above code, Letters have this values: Person[0].Letters[0]=0x55 and Person[0].Letters[1]=0x3; Problem solved. A. Riazi

          I 1 Reply Last reply
          0
          • A Abbas_Riazi

            As you know int takes 4 bytes in memory (in Win32), we can use union to do it for us.

            union _Person
            {
            char Letters[4];
            int dummy;
            } Person[5];

            OK, now you want to assign 10 bits to your Letters (for example 11 0101 0101 or 0x355). Person[0].dummy=0x355; after using above code, Letters have this values: Person[0].Letters[0]=0x55 and Person[0].Letters[1]=0x3; Problem solved. A. Riazi

            I Offline
            I Offline
            IrishSonic
            wrote on last edited by
            #5

            OK I think I understand it. But one thing I am confused about is where did u get 0x355,0x55 and 0x3. I think 0101 0101 is 5 bits each, so a total of 10 bits. Is this right?? So is 0101 0101 in hex form 0x55?? So where did u get 0x355?? And does these bits work the exact same way with ints?? Thanks, grahamoj

            A 1 Reply Last reply
            0
            • I IrishSonic

              OK I think I understand it. But one thing I am confused about is where did u get 0x355,0x55 and 0x3. I think 0101 0101 is 5 bits each, so a total of 10 bits. Is this right?? So is 0101 0101 in hex form 0x55?? So where did u get 0x355?? And does these bits work the exact same way with ints?? Thanks, grahamoj

              A Offline
              A Offline
              Abbas_Riazi
              wrote on last edited by
              #6

              grahamoj wrote: I think 0101 0101 is 5 bits each, so a total of 10 bits. Is this right?? No! each takes 4 bits. 0= 1 bit, 1= 1 bit. so 0101 0101= 8 bits. grahamoj wrote: So is 0101 0101 in hex form 0x55?? Yes. grahamoj wrote: So where did u get 0x355?? I use 10 bits (0x355 = 0011 0101 0101), then we need (actually) 12 bits. For this situation, 1.5 charachter required. OK, Because we have not any data type to store 1.5 character, we use 2 characters instead. I define a union:

              union _Person
              {
              char Letters[4]; //can save up to 4 characters;
              int dummy; //can save an integer value that takes 4 byte
              } Person[5]; //an array of union.

              when we set dummy, we also set Letters. Any bytes of dummy take one byte of Letters. If you look on C books, you will find useful information about union and it's benefits. A. Riazi

              1 Reply Last reply
              0
              • I IrishSonic

                I have done this: struct s_Person{ char Letter1 : 10, Letter2 : 10, Letter3 : 12; }Person[5]; What I want to do is have a string of 4 chars ( which is 32 bits ), and set the first 10 BITS of the string to be letter 'a' or something else... The struct fails because it says that the field types are too small. How do I put a character(or characters) into the first 10 bits of a string of 4 lenght?? Also, if the data field is only 10 bits, which is 2 chars + 2 bits, the maximum I could actually fit into Letter1 would be 2 letters..Is this right?? Thanks, grahamoj.

                J Offline
                J Offline
                Jambolo
                wrote on last edited by
                #7

                BTW, the type (except for signed/unsigned) is ignored for fields. The following are all exactly the same:

                struct s_Person { char Letter1 : 10, Letter2 : 10, Letter3 : 12; };
                struct s_Person { short Letter1 : 10, Letter2 : 10, Letter3 : 12; };
                struct s_Person { int Letter1 : 10, Letter2 : 10, Letter3 : 12; };
                struct s_Person { signed Letter1 : 10, Letter2 : 10, Letter3 : 12; };

                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