• C

Manipulation of bits

Hi:
    I'm writing a program to play with some networking stuff, but I'm having one nagging problem, some of my information comes back where the data I want is 4 bits in length. The other 4 bits that would come with the data I don't really care about so can be mangled if neccasary. I'm wondering how I get at these 4 bits I want( they're the latter 4 bits BTW) and then how to convert them into something meaningful such as one of the basic data types(char, int etc.) An algorithm is more what I'm looking for than having someone write the code for me. Thanks.
    Also, I'm recieving the bits in the form of a char.
LVL 1
jwilcoxAsked:
Who is Participating?
 
thresher_sharkCommented:
If you have any further questions, please feel free to ask.  Also, take my above comments as the "answer"
0
 
arunmCommented:
In what form are you receiving these bits?
0
 
jwilcoxAuthor Commented:
Edited text of question
0
Network Scalability - Handle Complex Environments

Monitor your entire network from a single platform. Free 30 Day Trial Now!

 
thresher_sharkCommented:
When you say latter 4 bits, I assume you mean something like this:

Four left most bits are junk, 4 right most you want.
0000 1010

If that is the case, try this:
eight_bit_variable <<= 4;
eight_bit_variable >>= 4;

Now, the value in the variable will be between 0 and 16, which is all you can do with 4 bits.

Is that what you wanted>?
0
 
thresher_sharkCommented:
You could also do this:

char mask = 31; // Four right bits set to 1;
// Could also be declared like so:
// mask = 8 | 4 | 2 | 1;

eight_bit_variable &= mask;

That will clear the left most bits (if I'm not mistaken).
0
 
thresher_sharkCommented:
Now that the other bits have been cleared, you can simply cast them to one of the other types:

// Place this after you use one of the above methods of clearing
// the bits.
your_integer_var = eight_bit_variable;

It is probably not necessary to cast them to an integer though since the largest value you can have in 4 bits is 15.
0
 
jwilcoxAuthor Commented:
I'm going to accept your answer, but I have a question first..shouldn't it be able to have a value > than 16?
0
 
thresher_sharkCommented:
I do not think so... with four bits, you can only represent 16 values:

0000 - 0
0001 - 1
0010 - 2
0011 - 3
0100 - 4
0101 - 5
0110 - 6
0111 - 7
1000 - 8
1001 - 9
1010 - 10
1011 - 11
1100 - 12
1101 - 13
1110 - 14
1111 - 15

For a total of 16 possibile values.
0
 
thresher_sharkCommented:
If you are talking about 8 bits, then there can indeed be up to 256 values (0-255).  If you still have any other questions, please feel free to ask them.  Thanks!
0
 
jwilcoxAuthor Commented:
I'm going to accept your answer, but I have a question first..shouldn't it be able to have a value > than 16?
0
 
jwilcoxAuthor Commented:
True but technically, as I've moved the right most bits to the left most bits My values are
actually 10000000 rather than 00001000. Anyways I'm just curious because the value I'm trying to get is supposed to be between 5 and 60.
0
 
jwilcoxAuthor Commented:
Nevermind, althought I'm sure now that its answered no one is looking at this, I finally, after spending a long time working with binary trying to get 60 while still having 5 as a possible value, figured out my source was wrong.
0
 
thresher_sharkCommented:
Sorry I couldn't have responded sooner, it was getting too late where I live.  Anyway, I'm glad everything worked out for you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.