ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
i usually write a function to decode the byte into a bool array,
and a function to convert a bool array into a byte.
manupilate individial bits between the 2 functions.
i am less familiar with c but i think it is still possible to do. You could take your byte and cast it into a boolean and the you should AND it with the number 4 also casted as a boolean.
My C is rusty, and my compiler doesn't recognise 'byte' or 'BYTE' (I assume I need to include something, but damned if I can remember), but char works just as well. I'm sure you can modify these to work with bytes easily enough.
Code:
void setbit( char *value, int indx ) {
*value = *value | (1 << indx);
}
void clearbit( char *value, int indx ) {
*value = *value & (255 - (1 << indx));
}
int main(void) {
char b = 40;
setbit( &b, 2 ); // Sets b to 44
clearbit( &b, 5 ); // Sets b to 12
return 0;
}
Another way to clear a bit that I haven't seen mentioned yet is:
value &= ~BIT_TO_UNSET;
The easiest way to set a bit, as mentioned already is:
value |= BIT_TO_SET;
Hex literals are pretty nice to use as the values for the bits you need because they are easy to work with.
0x01
0x02
0x04
0x08
0x10
0x20
0x40
0x80
etc...
So when you say the third bit, if you mean the bit that is set in 0x04 (As opposed to 0x08 if you meant 3rd bit using a typical 0-based index) just use:
Originally posted by tamoneya i am less familiar with c but i think it is still possible to do. You could take your byte and cast it into a boolean and the you should AND it with the number 4 also casted as a boolean.
here is the code in java.
Code:
(boolean)mybyte && (boolean)4
Umm... that code just type casts 4 and mbyte to be boolean, and then does a boolean and... the result would be TRUE or FALSE. And I don't think you can do that casting in java at all, since java's understanding of booleans does not include integer values.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.