reset password
Author Message
d.york
Posts: 7
Posted 19:58 Feb 27, 2014 |

I know we haven't covered using bitsets, but they make converting characters to ones and zeros really simple. Can we use them?

yash3ahuja
Posts: 5
Posted 20:02 Feb 27, 2014 |

You shouldn't need them. All characters and other integer types are implicitly 0's and 1's. 

For example, a char with the value of 8 is 0x08 = 00001000.

 

If you need to access a specific bit and set it to a given value, you can use the bit shift operator. For example, to change the above character to a value of 10, you could do: val | (0x01 << 1) = 00001000 | 00000010 = 00001010

Last edited by yash3ahuja at 20:22 Feb 27, 2014.
d.york
Posts: 7
Posted 20:20 Feb 27, 2014 |

Thank you. 

kknaur
Posts: 540
Posted 20:55 Feb 27, 2014 |

I would prefer that you didn't use a bitset.  It's better if you figure out how the bitwise operations work because it might be something you need to know for the future.

I would rather someone come to office hours and ask for help with the bitwise operations.

d.york
Posts: 7
Posted 21:06 Feb 27, 2014 |

Thank you professor. I was just curious.

kknaur
Posts: 540
Posted 21:15 Feb 27, 2014 |

Also check out this article

Bit Masking