Assuming I have char "C" whose ascii code is 0110 0111
. How can I iterate over its bits? I would like to build a vector from these 1's and 0's....
Asked
Active
Viewed 7,623 times
6

rodrigoap
- 7,405
- 35
- 46

Hamza Yerlikaya
- 49,047
- 44
- 147
- 241
6 Answers
13
You can easily iterate over them using bitwise operators:
char c = 'C';
for (int i = 0; i < 8; ++i)
{
// extract the i-th bit
int b = ((c & 1<<i) >> i);
// b will be 1 if i-th bit is set, 0 otherwise
// do whatever you want with b
}
you can optimize it (as suggested in comments):
int b = ((c >> i) & 1);

Jack
- 131,802
- 30
- 241
- 343
-
1Geez, guys, when the question looks THIS MUCH like homework, don't give code... Just a nudge in the right direction! – Bill K Mar 19 '10 at 20:58
-
1Cept from the OP's profile, it's fairly clear he's not a student. – Josiah Kiehl Mar 22 '10 at 20:58
3
A character has an integer value. Something like this will work :
int myChar = 42;
String binstr = Integer.toBinaryString(myChar);
The rest I'll leave to you as an exercise - but all you have to do now is iterate over the String representation of your binary value and do whatever it was that you planned on doing.

Amir Afghani
- 37,814
- 16
- 84
- 124
-
1The bitwise operations are faster and probably the better answer. The above solution is probably simpler to read. – Amir Afghani Mar 19 '10 at 20:30
2
Just use bitwise checks at each position you care about. Something like the following will create an array bits
that holds the individual values.
char c = 'C';
int[] bits = new int[8];
int j = 0;
for(int i = 1; i <= 256; i *= 2){
bits[j++] = (c & i) > 0 ? 1 : 0;
}

John Kugelman
- 349,597
- 67
- 533
- 578

Mark Elliot
- 75,278
- 22
- 140
- 160
-
1@Jeff, sure, but as it stands this is a decent, correct and readable answer. Such pendantry shouldn't merit a downvote. – Mark Elliot Mar 19 '10 at 21:08
-
I Didnt down vote. Actually I up voted and submitted an unrolled version of yours. Ppl should explain down votes. – Jeff Meatball Yang Mar 20 '10 at 02:01
0
You'll have to do this with bitwise operations:
ie:
while (my_char > 0) {
if my_char & 1
char_vector.push 1 // if the right most bit is 1
else
char_vector.push 0 // right most bit must be 0 if we fell through to the else
my_char = my_char >> 1 // right shift one position
}
if you need to, you can pad the char_vector with the remaining 0s, after you right shift to zero.

Josiah Kiehl
- 3,593
- 3
- 26
- 28
-
-
Forgive the c-like pseudocode. ;) Does it not make sense, or are you just mentioning that I didn't write syntactically correct code? (which of course, I didn't intend to) – Josiah Kiehl Mar 22 '10 at 20:56
0
char c = 'C';
Vector<Boolean> vector = new Vector<Boolean>(16);
for (int i = Character.SIZE-1; i >=0; --i) {
int num = c >> i;
boolean set = (num & 1) == 1;
vector.add(Boolean.valueOf(set));
}

Tom
- 43,583
- 4
- 41
- 61
0
Unrolled loop:
int[] bits = new int[8]
bits[0] = (c & 1) > 0 ? 1 : 0;
bits[1] = (c & 2) > 0 ? 1 : 0;
bits[2] = (c & 4) > 0 ? 1 : 0;
bits[3] = (c & 8) > 0 ? 1 : 0;
bits[4] = (c & 16) > 0 ? 1 : 0;
bits[5] = (c & 32) > 0 ? 1 : 0;
bits[6] = (c & 64) > 0 ? 1 : 0;
bits[7] = (c & 128) > 0 ? 1 : 0;

Jeff Meatball Yang
- 37,839
- 27
- 91
- 125