4

I have one charater pointer array which points to the some stirngs

every element points to some strings one thing in mind strings have only 1 and 0 .

for example

i have character pointer that stores string like "100110" so its takes 6 bytes to store but i want to store this in bits so i reduce memory occpuied.

4
  • 3
    C or C++ ? They are two different languages. Commented Oct 18, 2011 at 6:51
  • The << operator should help you. Commented Oct 18, 2011 at 6:51
  • Have a look into here :<stackoverflow.com/questions/688314/…> Commented Oct 18, 2011 at 6:51
  • in C .. and but << help ..i am fresher in C and i have some assignment work Commented Oct 18, 2011 at 6:59

4 Answers 4

3

In C style, something like this should work:

char* str = "100101";
unsigned out = 0;

for (int i = 0; str[i]; i++) {
   out = (out << 1);
   if (str[i] == '1')
      out++;
}

(I cannot test this for now, so please correct me if I'm wrong)

Explanation:

str = "100101"
out = 0000 0000 0000 0000

i = 0:
   out = 0000 0000 0000 0000 (out << 1)
   out = 0000 0000 0000 0001 (out++ because str[0] == '1')

i = 1:
   out = 0000 0000 0000 0010 (out << 1)

[...]

i = 5:
   out = 0000 0000 0010 0100 (out << 1)
   out = 0000 0000 0010 0101 (out++ because str[5] == '1')
Sign up to request clarification or add additional context in comments.

6 Comments

i will test. here unsigned out is char??
unsigned is for unsigned int, use the type you want, depending of your string length
can i use unsigned char out = 0;
an unsigned char is 1 byte (8 bits)
i want to implement huffman encoding algo after encoding i want to store my encoded value of every char in to somewhere and also reduce memory
|
3

Have you tried the STL bitset container? It's optimized for exactly this purpose. Alternatively, creating a vector of bool elements will internally pack bits to save space.

1 Comment

tell me more description about dis??
0

Why not use 'strtol'? It's a standard lib function. Or you're writing low-level code for memory shortage hardware?

char *str = "010101";
int i = strtol(str ,(char**)NULL ,2);

==> i=21

Comments

-1

One way is to, define that char* [] as a global and use its indices for accessing it:

char stringLiterals [] =  { "0101010", "10010010", "111", "010100100", ... };

Usage: Instead of

char *p = stringLiteral[3];

use

unsigned int idx = 3;

Rationale: If you are compacting this string into bits for serialization purpose than it's ok. But otherwise I don't see any use case of compacting them. In my above solution, it doesn't use any extra memory. You already have an array of string literals; I am just asking to declare in global scope and use its index.

Also, if the string size is > 32 bytes then, you won't be able to store it in a single 32-bit int.

11 Comments

i dnt want to access i want to store "0101010" string in bits. instead of bytes
@SaurabhPatel, yes I indeed get your question. My answer suggest you the final usability within your code. You store "010101" somewhere and then for using it you again covert it back to "010101". Instead of that, you can simply store that string literal into an array and just get its index. It's equivalent to converting "010101" into a single int. If you are not dealing with serializing your data then this technique is helpful. I assume that you want to covert the string literals and not the content of some variable, in the later case u have to use vector instead of array
@downvoter, please see my rational behind the answer in the comment above. If I am missing something, kindly explain.
@iammilind: "but i want to store this in bits so i reduce memory occpuied" (from its question). And I read you answer about 5 times to vaguely understand your point...
@Simon, and here it won't take any memory. Because he is just accessing the index of the already stored string literals. I don't find any use case of storing those literals into bits other than learning purpose (or serialization). Moreover, it is quite possible that the string length is > 32 bytes and the OP will not be able to store it in a single 32-bit int. :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.