Improve company productivity with a Business Account.Sign Up

x
?
Solved

Manipulation of bits

Posted on 1998-10-04
13
Medium Priority
?
264 Views
Last Modified: 2010-04-15
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
Comment
Question by:jwilcox
  • 7
  • 5
13 Comments
 
LVL 3

Expert Comment

by:arunm
ID: 1253320
In what form are you receiving these bits?
0
 
LVL 1

Author Comment

by:jwilcox
ID: 1253321
Edited text of question
0
 
LVL 6

Expert Comment

by:thresher_shark
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
Prepare for an Exciting Career in Cybersecurity

Help prevent cyber-threats and provide solutions to safeguard our global digital economy. Earn your MS in Cybersecurity. WGU’s MSCSIA degree program curriculum features two internationally recognized certifications from the EC-Council at no additional time or cost.

 
LVL 6

Expert Comment

by:thresher_shark
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;

eight_bit_variable &= mask;

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

Expert Comment

by:thresher_shark
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

by:
thresher_shark earned 800 total points
ID: 1253325
If you have any further questions, please feel free to ask.  Also, take my above comments as the "answer"
0
 
LVL 1

Author Comment

by:jwilcox
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

by:thresher_shark
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

by:thresher_shark
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

by:jwilcox
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

by:jwilcox
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

by:jwilcox
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

by:thresher_shark
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

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

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.

Join & Write a Comment

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

608 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question