Thread: Storing Multiple Bits as One Integer

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    6

    Storing Multiple Bits as One Integer

    So I'm supposed to write a code that asks a user for a string and then displays the hex, decimal, and binary code for each individual letter and then tells the user how many bits in binary were 1. For example:

    Enter a line of text: Hello

    The ASCII code for 'H' is 0x48 in hex, 72 in decimal, or 01001000 in binary, 2 bits were set.
    The ASCII code for 'e' is 0x65 in hex, 101 in decimal, or 01100101 in binary, 4 bits were set.
    The ASCII code for 'l' is 0x6c in hex, 108 in decimal, or 01101100 in binary, 4 bits were set.
    The ASCII code for 'l' is 0x6c in hex, 108 in decimal, or 01101100 in binary, 4 bits were set.
    The ASCII code for 'o' is 0x6f in hex, 111 in decimal, or 01101111 in binary, 6 bits were set.
    So far I've got a code that will display the binary bit pattern by shifting a mask and testing for a 1 or 0. The problem is I can't figure out how to make it so the 1's and 0's get put into a single integer rather than just printing out. I hope that makes sense. Here's my whole code.

    Code:
    #include<stdio.h>
    main ()
    {
      int i;
      char input;
    
      printf ("Enter ........: ");
      scanf ("%c", &input);
      for (i = 1; i <= 8; i++)
        {
          if (input & 0x80)
            printf ("1");
          else
            printf ("0");
          input = input << 1;
        }
    }
    Any help would be greatly appreciated.

  2. #2
    Registered User
    Join Date
    Sep 2013
    Posts
    6
    Output:
    Enter .......: a
    01100001%

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    6
    Okay I just thought of something. Is there a more efficient way to do it than this:
    Code:
    main ()
    {
      int i;
      char input;
      int binary;
      int bit;
      printf ("Enter ........: ");
      scanf ("%c", &input);
      for (i = 1; i <= 8; i++)
        {
          if (input & 0x80)
            bit = 1;
          else
            bit = 0;
    if (i == 1)
    binary = binary + bit * 10000000;
    else if (i == 2)
    binary = binary + bit * 1000000;
    else if (i == 3)
    binary = binary + bit * 100000;
    else if (i == 4)
    binary = binary + bit * 10000;
    else if (i == 5)
    binary = binary + bit * 1000;
    else if (i == 6)
    binary = binary + bit * 100;
    else if (i == 7)
    binary = binary + bit * 10;
    else if (i == 8)
    binary = binary + bit;
          input = input << 1;
        } 
    printf("%d", binary);
    }
    Also, this works but I'm missing my leading 0.

    Output:
    Enter ........: a
    1100001%

    'a' in binary is 01100001

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Don't do it that way!!!

    As you feared, your question doesn't make sense. A char (or unsigned char) is just a little integer, so it's already a binary value. You actually just want to print out 1's and 0's like you're already doing, although you also want to count the 1 bits.

    Also, you should use "unsigned char" to ensure an unsigned value since shifts on signed values are not recommended.

    And your main should be "int main(void)" and you should "return 0;" at the end of it.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by Lilith Matthews View Post
    I can't figure out how to make it so the 1's and 0's get put into a single integer.
    You already have that, each character in the string contains a set of 8 binary digits (1's or 0's).

    Quote Originally Posted by Lilith Matthews View Post
    how many bits in binary were 1.
    You can use this code sequence to clear the least significant bit, no matter where it is: x = (x) & (x - 1); (assuming two's complement processor) . You can use this in a while(x != 0) and count the number of loops to determine the number of bits. Newer X86 based processors have an instruction that counts the number of 1 bits, and some compilers have an intrinsic function that will take advantage of this if the instruction is present, with names like popcnt(), __popcnt64(), ... .

  6. #6
    Registered User
    Join Date
    Sep 2013
    Posts
    6
    Okay I'm clearly not making sense so I'm going to copy/paste the problem.

    Ask the user to enter a line of text and then read it in a character at a time. For each character, print the character as a character, its hex value, its decimal value, and its binary value. (One of the philosophic points of this assignment is remind you that there are no numbers or characters inside the computer; there are just binary bit patterns that have no intrinsic meaning until your program tells the computer how to interpret/manipulate them.) Just to give you a little more work (it's unnecessary to thank me ;-) also count and print the number of bits that are set to 1 in the binary. Here's a sample output:
    Enter a line of text: Hello there!
    The ASCII code for 'H' is 0x48 in hex, 72 in decimal, or 01001000 in binary, 2 bits were set.
    The ASCII code for 'e' is 0x65 in hex, 101 in decimal, or 01100101 in binary, 4 bits were set.
    The ASCII code for 'l' is 0x6c in hex, 108 in decimal, or 01101100 in binary, 4 bits were set.
    The ASCII code for 'l' is 0x6c in hex, 108 in decimal, or 01101100 in binary, 4 bits were set.
    The ASCII code for 'o' is 0x6f in hex, 111 in decimal, or 01101111 in binary, 6 bits were set.
    The ASCII code for ' ' is 0x20 in hex, 32 in decimal, or 00100000 in binary, 1 bits were set.
    The ASCII code for 't' is 0x74 in hex, 116 in decimal, or 01110100 in binary, 4 bits were set.
    The ASCII code for 'h' is 0x68 in hex, 104 in decimal, or 01101000 in binary, 3 bits were set.
    The ASCII code for 'e' is 0x65 in hex, 101 in decimal, or 01100101 in binary, 4 bits were set.
    The ASCII code for 'r' is 0x72 in hex, 114 in decimal, or 01110010 in binary, 4 bits were set.
    The ASCII code for 'e' is 0x65 in hex, 101 in decimal, or 01100101 in binary, 4 bits were set.
    The ASCII code for '!' is 0x21 in hex, 33 in decimal, or 00100001 in binary, 2 bits were set.


    I've already got the way to get the binary representation printed out, I just need it to be stored in one place. See the code in the first post.

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    But why do you need it "to be stored in one place"? That's what doesn't make sense. Just make a function out of your code and call it at the right point, something like this:
    Code:
    printf("The ASCII code for '%c' is 0x%x in hex, %d in decimal, or ", c, c, c);
    num_bits = print_binary(c);
    printf(" in binary, %d bits were set.\n", num_bits);
    This is assuming that the print_binary function also counts the bits and returns that value. You could even write it like this:
    Code:
    printf("The ASCII code for '%c' is 0x%x in hex, %d in decimal, or ", c, c, c);
    printf(" in binary, %d bits were set.\n", print_binary(c));
    But that's less intuitive.

    EDIT: However, if you have your heart set on collecting all your 1's and 0's together before printing them, it would be best to store them in a string, not an integer.
    Last edited by oogabooga; 10-03-2013 at 06:13 PM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  8. #8

  9. #9
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by Lilith Matthews View Post
    Okay I'm clearly not making sense so I'm going to copy/paste the problem.

    Ask the user to enter a line of text and then read it in a character at a time. For each character, print the character as a character, its hex value, its decimal value, and its binary value. I just need it to be stored in one place. See the code in the first post.
    As mentioned before, the binary value of each character is already stored in "one place", in the character itself.


  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You need to explicitly use unsigned chars for this. In fact, any time you use the bit-wise shift operators (<< and >>), you need to use unsigned types. The reasons for your case (using char) are two-fold.

    First, the standard dictates that whether a char is signed or unsigned is implementation defined. That means, unless you know for sure on your implementation, you may have signed chars. Using unsigned char guarantees that (surprise) it's unsigned. And even if you do know that, on your system, chars are unsigned, that still means that your code is not portable. It's very easy to avoid all problems by just declaring variables as unsigned char.

    Second, is that you are using the bit-wise shift operators. They work great with unsigned chars, but if you have signed chars, you have to consider the following (I emboldened the important parts):
    Quote Originally Posted by C11 6.5.7 p4-5
    4 The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are filled with
    zeros. If E1 has an unsigned type, the value of the result is E1×2E2, reduced modulo
    one more than the maximum value representable in the result type. If E1 has a signed
    type and nonnegative value, and E1×2E2 is representable in the result type, then that is
    the resulting value; otherwise, the behavior is undefined.

    5 The result of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an unsigned type
    or if E1 has a signed type and a nonnegative value, the value of the result is the integral
    part of the quotient of E1/2E2. If E1 has a signed type and a negative value, the
    resulting value is implementation-defined.
    Basically, what those sentences say is that, if E1 is signed type, and negative, undesireable things may occur. Undefined behavior is bad and should be avoided. Implementation-defined behavior, while safe, is specific to that implementation, meaning your code may break on every other implementation. It's certainly not safely portable.
    Last edited by anduril462; 10-03-2013 at 07:42 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. storing data in bits.
    By ak47 in forum C Programming
    Replies: 7
    Last Post: 11-30-2012, 10:51 PM
  2. Copying 32 bits into 16 bit integer???
    By blackCat in forum C Programming
    Replies: 4
    Last Post: 02-17-2009, 10:00 AM
  3. Storing a float in 16 bits
    By kara3434 in forum C Programming
    Replies: 7
    Last Post: 07-31-2007, 01:34 PM
  4. How many bits are '1' in an integer variable?
    By cuthbert in forum C Programming
    Replies: 51
    Last Post: 09-13-2006, 04:14 PM
  5. How many bits are '1' in an integer variable?
    By cuthbert in forum C++ Programming
    Replies: 0
    Last Post: 09-12-2006, 01:08 PM

Tags for this Thread