LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-23-2005, 08:44 PM   #1
blackzone
Member
 
Registered: Jun 2004
Posts: 256

Rep: Reputation: 30
c programming stupid question of the day


I have a byte.

is a function that let me set the 3rd bit of the byte? instead of manual adding 2^(3-1)?
 
Old 01-23-2005, 09:23 PM   #2
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
how about

Code:
if (byte & (1 << 4))
    /* byte is set */
that's as good as it gets, unless you write the function/macro yourself.
 
Old 01-23-2005, 09:24 PM   #3
qwijibow
LQ Guru
 
Registered: Apr 2003
Location: nottingham england
Distribution: Gentoo
Posts: 2,672

Rep: Reputation: 47
no that i know of.

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.
 
Old 01-23-2005, 09:44 PM   #4
tamoneya
Member
 
Registered: Jan 2005
Location: MA
Distribution: Ubuntu 7.10
Posts: 558

Rep: Reputation: 31
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
 
Old 01-23-2005, 09:45 PM   #5
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
Quote:
instead of manual adding 2^(3-1)?
Adding a value like that will only set the bit if the bit is currently unset. If it's already set, you will get extremely unwelcome side effects.

What you want to do is:
byte = byte | (1 << n)
where n is the (zero based) index of the bit.
 
Old 01-23-2005, 09:52 PM   #6
tamoneya
Member
 
Registered: Jan 2005
Location: MA
Distribution: Ubuntu 7.10
Posts: 558

Rep: Reputation: 31
oops instead of anding (boolean)4 you should actually AND (boolean)251 which is 11111011 in binary. this will turn the third bit to zero.
 
Old 01-23-2005, 09:52 PM   #7
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
hah, I read "set" as "test". oh well. Do what CroMagnon said.
 
Old 01-23-2005, 10:15 PM   #8
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
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;
}
 
Old 01-24-2005, 08:34 AM   #9
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
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:

value |= 0x04; // set
value &= ~0x04; // unset
 
Old 01-25-2005, 12:47 PM   #10
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
Quote:
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Stupid, stupid question; I lost Klaptop. :( Surfrider Slackware 2 08-31-2005 09:12 PM
stupid questoin of the day blackzone Linux - Networking 1 01-23-2005 07:27 AM
Stupid Dumb Stupid Question... drigz Linux - Software 3 09-23-2004 03:09 PM
First Day, First Question Bi0mic Linux - Newbie 6 02-26-2004 12:49 PM
Stupid Newbie Question of the day(d/loading Linux) chuckeff Linux - General 3 03-27-2003 06:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration