Solved

# Manipulation of bits

Posted on 1998-10-04
253 Views
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.
0
Question by:jwilcox
• 7
• 5

LVL 3

Expert Comment

ID: 1253320
In what form are you receiving these bits?
0

LVL 1

Author Comment

ID: 1253321
Edited text of question
0

LVL 6

Expert Comment

ID: 1253322
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

LVL 6

Expert Comment

ID: 1253323
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;

That will clear the left most bits (if I'm not mistaken).
0

LVL 6

Expert Comment

ID: 1253324
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

LVL 6

Accepted Solution

thresher_shark earned 200 total points
ID: 1253325
0

LVL 1

Author Comment

ID: 1253326
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

LVL 6

Expert Comment

ID: 1253327
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

LVL 6

Expert Comment

ID: 1253328
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

LVL 1

Author Comment

ID: 1253329
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

LVL 1

Author Comment

ID: 1253330
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

LVL 1

Author Comment

ID: 1253331
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

LVL 6

Expert Comment

ID: 1253332
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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question