# 2 bitwise questions

This is a discussion on 2 bitwise questions within the C Programming forums, part of the General Programming Boards category; 1. Can I access each bit of value stored in a byte? Supposing an integer value 65 (int i =65 ...

1. ## 2 bitwise questions

1. Can I access each bit of value stored in a byte?
Supposing an integer value 65 (int i =65 is stored in 2 bytes of memory, I can easily find out its memory location by using pointers. We know that within 2 bytes(for 16-bit environment) it will be stored as 1000001, but can I find out this data in each bit of these two bytes?

2. How does the bitwise complement (~) operators work with signed int or char? I can understand with unsigned values. Plz explain how it works with signed int or char.

2. Originally Posted by kirtikjr
1. Can I access each bit of value stored in a byte?
Yes. Have at look at http://www.cs.umd.edu/class/sum2003/.../bitshift.html

3. Code:
```#include <stdio.h>

int main () {
int bytesw=0, unset;
const int A=1,B=1<<1,C=1<<2;

bytesw = bytesw | A;
printf("A set: %d\n",bytesw);
unset = ~ A;
bytesw = bytesw & unset;
printf("A unset: %d\n",bytesw);
bytesw = bytesw | B;
printf("B set: %d\n",bytesw);
unset = ~ B;
bytesw = bytesw & unset;
printf("B unset: %d\n",bytesw);
bytesw = bytesw | C;
printf("C set: %d\n",bytesw);
unset = ~ C;
bytesw = bytesw & unset;
printf("C unset: %d\n",bytesw);
bytesw = bytesw | C | A;
printf("AC set: %d\n",bytesw);
}```
Output:
Code:
```A set: 1
A unset: 0
B set: 2
B unset: 0
C set: 4
C unset: 0
AC set: 5```

4. Originally Posted by kirtikjr
2. How does the bitwise complement (~) operators work with signed int or char? I can understand with unsigned values. Plz explain how it works with signed int or char.
I don't think that makes much difference:

Code:
```#include <stdio.h>

int main() {
char X=0;
unsigned int alpha=6<<4, beta=3<<2, tmp;
X = X | alpha;
X = X | beta;
tmp=1<<0;       // 00000001
X = X | tmp;
printf("%c",X);
tmp = ~ beta;     // invert bits 00001100 to 11110011
X = X & tmp;      //01100110 ANDED with 11110011
tmp=10<<0;      // 00001010
X = X | tmp;
printf("%c\n",X);
return 0;
}```
produces the same output if you use regular signed ints.

Here's a better page: http://www.space.unibe.ch/comp_doc/c...bit_shift.html

5. The ~ operator does the same BITWISE operation on a signed and an unsigned operand - the operator does not care one bit about whether it is a signed or unsigned value. It just flips all the bits around to the opposite value (all zeros becomes ones, and ones become zeros).

--
mats