Change Specific Value Of A Bit


 
Thread Tools Search this Thread
Top Forums Programming Change Specific Value Of A Bit
# 1  
Old 03-21-2008
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 ?
# 2  
Old 03-21-2008
Kinda vague - do you want to turn the bit on? off? flip the bit?
# 3  
Old 03-21-2008
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  
Old 03-21-2008
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  
Old 03-21-2008
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  
Old 03-21-2008
thanks in advance
# 7  
Old 03-21-2008
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");
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using awk to change a specific column and in a specific row

I am trying to change the number in bold to 2400 01,000300032,193631306,190619,0640,1,80,,2/ 02,193631306,000300032,1,190618,0640,CAD,2/ I'm not sure if sed or awk is the answer. I was going to use sed and do a character count up to that point, but that column directly before 0640 might... (8 Replies)
Discussion started by: juggernautjoee
8 Replies

2. Shell Programming and Scripting

how to change specific value for a entry in the file

Hello All, can someone please suggest me a one line command to change a specific value that is associated to an entry in the file. for example #more schedulefile quartz.job.manual.bonus.schedule=0 0 9 ? * * # it should be changed to #more schedulefile... (5 Replies)
Discussion started by: bobby320
5 Replies

3. Shell Programming and Scripting

change character(s) in specific column

Hi all! I need to change the final e every time when it is present in any word in column 1 to a; moreover, to change the final i again to a in any word in column 1, but just if word in column 2 begins with ha or si. Here below you can see a sample of my data: achwa ungeliachwa ungeli 1... (3 Replies)
Discussion started by: mjomba
3 Replies

4. Shell Programming and Scripting

Change specific occurence with sed

Hello, I have this file. aaa port=1234 time bbb port=2233 name ccc port=4444 name Is there any way with sed to change only the occurence of "port" which comes after section to have as output : (12 Replies)
Discussion started by: rany1
12 Replies

5. Shell Programming and Scripting

Change specific ip address in a file

I need to change a line from a xen cfg file using sed if it's possible. The original line is: vif = I want to change ONLY the IP address of the second part ==> ip=10.1.10.4 to another IP --> ip=192.222.11.6 The first one ip=123.456.789.123 keeps untouchable. My new line shoud... (9 Replies)
Discussion started by: iga3725
9 Replies

6. UNIX for Dummies Questions & Answers

Change Specific Line of a File

Hi everyone, I am attempting to do something that should be very simple. How do I replace a specific line of a file with different text, and then save that file to its original name? I believe I want to use the sed command with the c option, but I after trying many times, I can't get the right... (10 Replies)
Discussion started by: msb65
10 Replies

7. Shell Programming and Scripting

How to change a specific character in a file

Hi, I have a data file with following structure: a|b|c|d|3|f1|f2|f3 a|b|c|d|5|f1|f2|f3|f4|f5 I want to change this data to: a|b|c|d|3|f1;f2;f3 a|b|c|d|5|f1;f2;f3;f4;f5 Data in column 5 tells the number of following fields. All fields delimiter after the 5th column needs to be... (6 Replies)
Discussion started by: sdubey
6 Replies

8. Shell Programming and Scripting

a bit tricky to change it multiple rows in one row and ...

Hi, I have an output file like: 1415971694 376 (12); 3434327831 376 (7); 2989082873 332 (3); 4075357577 332 (3); 1221064374 376 (2); 2372410470 376 (2); 2563564778 332 (2); 443221432 376 (1); ... (2 Replies)
Discussion started by: netbanker
2 Replies

9. Shell Programming and Scripting

change the format of a giving file ( a bit challenge) Thank you

Hi, I have a file like this: mgr1.dbf tool.dbf usr.dbf wow19.dbf wow2wow.dbf Can anyone help change the format after each line of reading to: mgr2.dbf # add 1 of *1.dbf tool2.dbf # if not a number exist, make it default to 2 usr2.dbf wow20.dbf # add 1 of *19.dbf wow3wow.dbf ... (5 Replies)
Discussion started by: netbanker
5 Replies

10. HP-UX

Change 32 bit to 64 bit Kernel

Hi, I'm using HPUX 11.11 on Vizualize B180L our productive machines have 64 bit kernel # file vmunix vmunix: ELF-64 executable object file - PA-RISC 2.0 (LP64) I got one test system, which should have the same HW (i was told it is the same HW) hpuxtest:/stand-->file... (3 Replies)
Discussion started by: funksen
3 Replies
Login or Register to Ask a Question