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. #1
    Registered User kirtikjr's Avatar
    Join Date
    Nov 2008
    Posts
    4

    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. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by kirtikjr View Post
    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
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by kirtikjr View Post
    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
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Bitwise Questions
    By someprogr in forum C Programming
    Replies: 8
    Last Post: 12-14-2008, 05:45 PM
  3. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 08:44 AM
  5. Bitwise Test Questions
    By Mister C in forum C Programming
    Replies: 9
    Last Post: 11-27-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21