-
Passing an 8-bit String
I've been asked to write some code that is unlike anything I've had to do before, so I'm looking for suggestions on how I should go about doing this.
I need to have an 8-bit string where the first 4-bits represent the command (either erosion or dilation of an image...those 2 functions are already written) and the last 4-bits represent the one parameter that those functions take (an int value representing the size). I have to pass that string into a new function that calls either the erosion or dilation methods based on the first 4-bits of the string with the appropriate parameter from the last 4-bits of the string. The 8-bit string is terminated at null with all 0's. The person who explained it to me is not in for a few days, so I'm at a big dilemma, and don't know where to begin. He wrote down that the 8-bit string could look like this:
command | parameter
0001 0001 (i think this one represented erosion)
0010 0001
0000 0000
and that the 8-bit string could be hard coded for now for testing purposes. I just don't understand how the method that I need to write will be able to "extract" the first 4-bits to represent either the erosion or dilation method and then "extract" the last 4-bits for the parameter and also know if there are multiple commands within the string and handle those until there is a null terminator. I'm not even sure how he wants me to hard code this string. Do I represent it with the binary representation? If anybody has any idea of where I could start with this, it would be really appreciated. Thanks
-
Well the typical way to do this would be to use an int (or even a char), and hex
numbers - two hex digits represent 8 bits, and to maximise efficiency you could
cast to a char. Then you can use bitwise operators to extract the data from the
two nibble's (or nybble as I like to spell it).
It seems that you may not be allowed to do it this way, so if you have to
use a string, look up strncpy, strcmp and in the event that the low nibble
can have any one of 16 possible values, use strtol to access the numerical
data rather than dealing with ASCII values for 1 and 0.
-
Okay, then my next question is, or maybe I didn't fully understand, what would I have to do to match up the first hex digit with the correct method and the second hex digit with the correct parameter so that I would be able to call them after I used bitwise operators to extract the hex digits from the string? Do I some how assign those methods and parameters to a hex digit or do I use switch or if statements to call the methods based on the data?
Thank you for your help. This problem is obviously beyond the programming knowledge that I've been use to.
-
Bitwise "extraction" (binary AND):
Code:
char bits = 0xFF; // bits = 1111 1111
if (bits & 0xF0){ // extracts the four bits in the command part
...
}
For more information I suggest you ask everyone's best pal google something like "c++ binary operands".
-
I assume that you are using an int to store your "string" of bits, so I've written
this small example to demonstrate how you might extract your data.
Code:
#include <stdio.h>
/*some definitions of values*/
#define BITMASK 0xF
#define NIBBLE 4
int main (void)
{
int a_byte = 0xA8; /*some generic value*/
int temp_var1 = 0;
int temp_var2 = 0;
temp_var1 = a_byte & BITMASK; /*this line uses the & operator to isolate the lower bits*/
temp_var2 = (a_byte >> NIBBLE) & BITMASK; /*this then shifts the upper bits and isolates them */
printf ("Our byte was %x in hex\n\n", a_byte, a_byte, a_byte);
printf ("The lower nibble was %x\n\n", temp_var1, temp_var1);
printf ("The upper nibble was %x\n\n", temp_var2, temp_var2);
/*now we could use the values in temp_var1 and temp_var2 in switch statements, function parameters etc*/
getchar ();
}
So considering your date format, you can use the upper nibble in a switch
statement (temp_var2), and the lower nibble will always be your data parameter
(temp_var1). Hope this helps.
-
Richie,
Thanks for the example. Things are starting to make a lot more sense in terms of how I need to manipulate my data. The last question I have is in regards to your code. What if you have more definitions of values? For instance:
#define BITMASK 0xF
#define BITMASK1 0xA
#define NIBBLE 1
#define NIBBLE2 2
#define NIBBLE3 3
#define NIBBLE4 4
Would these two lines change?
temp_var1 = a_byte & BITMASK;
temp_var2 = (a_byte >> NIBBLE) & BITMASK;
Because I have something similar to the above 6 definitions, and I will be using a switch statement where one of the NIBBLE's will be the parameter.
-
I think you misunderstood what Riche was doing...
-
pseudocode time:
Code:
0b00100110 == 38 or 0x26
0b1111 == 15 or 0xF
0b00100110 & 0b1111 == 0b0110 or 0x6 or 6
0b00100110 >> 4 == 0b00000010 /*notice that it shifted over so that the Hi nibble is now the Lo nibble */
0b00000010 & 0b1111 == 0b0010 or 0x2 or 2
-
Thank you for the breakdown. I have everything working in my project and the application works appropriately with all of your suggestions. Thank you again!