views:

191

answers:

6

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....

+2  A: 

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
The bitwise operations are faster and probably the better answer. The above solution is probably simpler to read.
Amir Afghani
+3  A: 

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
Jimmy
Geez, guys, when the question looks THIS MUCH like homework, don't give code... Just a nudge in the right direction!
Bill K
Cept from the OP's profile, it's fairly clear he's not a student.
Josiah Kiehl
yes, but downvote is always so tempting to press :)
Jack
A: 

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;
}
Mark E
Could "i *= 2" be changed to "i << 1", I wonder?
Jeff Meatball Yang
@Jeff, sure, but as it stands this is a decent, correct and readable answer. Such pendantry shouldn't merit a downvote.
Mark E
I Didnt down vote. Actually I up voted and submitted an unrolled version of yours. Ppl should explain down votes.
Jeff Meatball Yang
A: 

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
This doesn't look Java to me :-/
Péter Török
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
A: 
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
A: 

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