?
Solved

Manipulation of bits

Posted on 1998-10-04
13
Medium Priority
?
258 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

762 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