I want to know the bit values or say binary values in a character. for example for a binary value is 01100001 i want to store each '0' and '1' in an array! plz tell me how to access it bitwise.
Asked
Active
Viewed 456 times
-3
-
Storing the bits of something in an array is usually a sign of a bad plan. Not always, but usually. What are you planning to use this for? – harold Oct 22 '14 at 14:53
-
3You can get the bits of *anything* by using the bitwise shift and bitwise and operators. However, you might first want to [read about the XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem), and tell us *why* you want to do it. – Some programmer dude Oct 22 '14 at 14:55
-
You can iterate the length (sizeof(char)) of the char while shifting and storing the results in an array – Rotem Varon Oct 22 '14 at 14:55
-
i want to add all the bit values and find an avg. but i don't know how to access... – Deepak Singh Oct 22 '14 at 14:55
-
1if u need an AVG u can skip the array and just count the 0s ans 1s with two ints – Rotem Varon Oct 22 '14 at 14:57
-
1So you want to count the bits that are one? (and then maybe divide by something) See, storing them in an array was a bad plan. Instead, see [popcnt](http://stackoverflow.com/questions/109023/how-to-count-the-number-of-set-bits-in-a-32-bit-integer) – harold Oct 22 '14 at 14:57
-
1@RotemVaron rather `CHAR_BIT` in case `char` is not 8 bits long. – The Paramagnetic Croissant Oct 22 '14 at 14:57
-
An average of *what*? The number of `1` bits? The number of `0` bits? None of those will really tell you anything about a character or a string, so please tell us why you want this. – Some programmer dude Oct 22 '14 at 14:58
-
i just want to know how to access 1's and 0's in a char. and then calculate the avg. i don't want to store it in any array!! just how to access bitwise – Deepak Singh Oct 22 '14 at 14:59
-
1And regarding the comment from @TheParamagneticCroissant, sizeof(char)` multiplied by `8` will not work, as `sizeof(char)` is specified to always return `1` no matter the number of bits in a `char`. And besides, the number of bits in a `char` doesn't have to be a multiple of `8` which make using `sizeof` moot anyway. – Some programmer dude Oct 22 '14 at 15:00
-
1How does "i don't want to store it in any array!!" rhyme with the original "i want to store each '0' and '1' in an array!"? Is the number of exclamation marks significant? – Jongware Oct 22 '14 at 15:39
-
OP has told us "I want to store each '0' and '1' in an array", and then "I want to add all the bit values and find an avg", and then contradicts with "I don't want to store it in any array". So it's back to @Joachim Pileborg's comment about the XY problem. – Weather Vane Oct 22 '14 at 15:45
2 Answers
1
For example
#define N 8
//...
char c = '\x61';
int b[N] = { 0 };
size_t i = N;
for ( unsigned char ch = c; ch; ch >>= 1 )
{
b[--i] = ch & 1;
}

Vlad from Moscow
- 301,070
- 26
- 186
- 335
0
Building on @Vlad from Moscow's answer and OP's latest requirement to find the average bit value
#include <stdio.h>
int main()
{
unsigned char ch, c = '\x61';
double tot = 0;
for (ch = c; ch; ch >>= 1)
tot += (ch & 1);
printf ("Average bit value is %f\n", tot / 8);
return 0;
}

Weather Vane
- 33,872
- 7
- 36
- 56
-
Thank you very much! This is what i was looking for the whole time. Appreciated. – Deepak Singh Oct 27 '14 at 19:14