The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
need aix commands specific for sap p372707 UNIX for Dummies Questions & Answers 1 05-14-2008 11:58 PM
How to change a specific character in a file sdubey Shell Programming and Scripting 6 02-22-2008 12:30 PM
hp -specific mxms755 HP-UX 1 05-15-2006 09:21 AM
specific date on the log ahmad_one Shell Programming and Scripting 1 03-26-2006 05:49 AM
how to get specific xml tag ? forevercalz Shell Programming and Scripting 13 01-09-2006 12:37 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 03-21-2008
Registered User
 

Join Date: Mar 2008
Posts: 3
Change Specific Value Of A Bit

char character has lenght 4 bytes .that means that is is 32 bit. how can ichange a value of a specific bit , for example the 15 bit of this sequence of 32 bit in C ?
Reply With Quote
Forum Sponsor
  #2  
Old 03-21-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,298
Kinda vague - do you want to turn the bit on? off? flip the bit?
Reply With Quote
  #3  
Old 03-21-2008
Registered User
 

Join Date: Mar 2008
Posts: 3
i want to make it 1 or 0 .i want to do this because when i have a file with 50 blocks ,if i want to know if the 30 block has data , so i will make the
30st bit of the first block of the file 1.
Reply With Quote
  #4  
Old 03-21-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,298
Your answer still was not clear. I think you want this:
Code:
x = x ^ mask; 
(or x ^= mask;) 
Bits that are set to 1 in the mask will be flipped in x. 
Bits that are set to 0 in the mask will be unchanged in x. 

Toggling (flipping) means that if the bit is 1, it is set to 0, and if the bit is 0, it is set to 1. 

XOR truth table
0 ^ 0 = 0
1 ^ 0 = 1
0 ^ 1 = 1
1 ^ 1 = 0
Reply With Quote
  #5  
Old 03-21-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,298
if you want to see which bit is set:
Code:
if(value & 1 ) -> true if first bit is set
if(value & 2 ) second bit
if(value & 4) third
if(value & 8) fourth
if(value & 16) fifth
... and so on
Reply With Quote
  #6  
Old 03-21-2008
Registered User
 

Join Date: Mar 2008
Posts: 3
thanks in advance
Reply With Quote
  #7  
Old 03-21-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 567
What platform are you on? All char type variables have a width of 8-bits so I'm not sure how you are getting a size of 32-bits for variables of char type.

Code:
#include <stdio.h>

int main(int argc, char **argv)
{
    unsigned long i, j;
    unsigned long var;    /* variable whose 15th bit you want to test */
    
    i = 0100000;          /* unsigned octal has 15th bit set to 1 and the rest are 0 */
    j = var & i;

    if (j == i)           /* if the 15th bit of var is set to 1 then j will equal i */
        printf("15th bit is set to 1\n");
    else                  /* if the 15th bit of var is set to 0 j will equal zero */
        printf("15th bit is set to 0\n");
}
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 12:49 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0