|
| chris (70) | | ||||||||||||
| This is just a little handy structure defining a variable type & a simple operator overload, to get a bit value from a string. First things first we declare a structure, so:
Next we need to create a private variable to accept a byte, so we use an unsigned char & let's call it "val":
We need to be able to give the byte a value, so let's create the subroutine to accept the byte & set it to our private variable:
And last, but not least an operator overload so that we can decide which bit we want:
To quickly explain that above, perhaps complex looking, bitwise statement - it takes a number (i) & then finds if that bit is 1 or 0, then it makes the return value equal to that (1 or 0). So our completed structure is:
Now - how to use this, in this example we will take an unsigned char variable called "a" holding the ASCII value of "a", which is 97 - then find all the bits of it.
From this we should get "01100001" - 97 in binary. I hope this is some help to anyone really, & is my first attempt at an article - any feedback or suggestions are welcome. Chris. | |||||||||||||
| firedraco (1967) | |
| I think you might want to upgrade to C++ iostream, etc instead of printf() etc, since if you are using classes then you have to be using C++. | |
Last edited on | |
| kbw (1032) | | ||
| You don't need the right shift in the index operator. And the index should probably be an int rather than a char as it's more efficient. The char will be pushed as an int and used as a char in the called function.
Thanks for posting the article. | |||
| chris (70) | |
| Oddly enough firedraco I used to always use C++ iostream, I just seem to find the << & >> used as input / output slightly annoying =P Thanks kbw, I will take into account the unsigned int instead of char - I use the right shift so that the function always returns a 1 or a 0. Thanks for all feedback, I might do another bitwise article sometime ;) | |
This topic is archived - New replies not allowed.

