![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 ?
|
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
Kinda vague - do you want to turn the bit on? off? flip the bit?
|
|
#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. |
|
#4
|
|||
|
|||
|
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 |
|
#5
|
|||
|
|||
|
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 |
|
#6
|
|||
|
|||
|
thanks in advance
|
|
#7
|
|||
|
|||
|
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");
}
|
|||
| Google The UNIX and Linux Forums |