Thread: Bit operator in c programming

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    128

    Bit operator in c programming

    In the c programming, operations can be performed on a bit level using bit wise operator

    Bits that are 0 become 1, and those that are 1 become 0. For example:

    bit a ~a
    0 1
    1 0

    I have used bit wise operator in program output should be 0 but it show output -2. why does it show -2 ?
    Code:
    #include<stdio.h>
     
    int main()
    {
                
       int a = 1;
     
       printf(" addition   %d \n", ~a);
     
       return 0;
    }
    Result -2

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    It shows -2 because the computer uses two's complement to represent negative numbers, and the ~ operator inverts all the bits, not just the first one.

    Long story short, the negative of a binary number "n" can be represented as "~n + 1"
    Last edited by GReaper; 10-16-2017 at 01:28 AM.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    128
    Quote Originally Posted by GReaper View Post
    It shows -2 because the computer uses two's complement to represent negative numbers, and the ~ operator inverts all the bits, not just the first one.

    Long story short, the negative of a binary number "n" can be represented as "~n + 1"
    Thank you for your reply. I was trying to write c program for following condition with bitwise operator

    bit a ~a
    0 1
    1 0

    How to test above condition ?

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    If you want only a single bit to be displayed, you need a bit-mask, like this:
    Code:
    (~a & 1)
    Devoted my life to programming...

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    I'm not really sure I understand what you're getting at, but if you want to make 1 into binary 10, the simplest thing to do is add.

    You could also xor 3, since 1 xor 3 = 2.

  6. #6
    Registered User
    Join Date
    Jun 2017
    Posts
    88
    I found the windows calculator very helpful in understanding bitwise. You can just switch it from Standard to Programmer (or use the hotkey Alt 3). It then shows your calculations in binary. It also allows you to use bitwise operators.
    Last edited by jack jordan; 10-19-2017 at 07:17 PM.

  7. #7
    Registered User
    Join Date
    Apr 2017
    Location
    Iran
    Posts
    103
    Quote Originally Posted by abhi143 View Post
    Thank you for your reply. I was trying to write c program for following condition with bitwise operator

    bit a ~a
    0 1
    1 0

    How to test above condition ?
    Why do not you use unsigned rather than signed values ?

    AFAIK you rarely use bit-twiddling with signed values.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 12-26-2016, 07:45 AM
  2. Replies: 6
    Last Post: 12-10-2012, 11:36 AM
  3. New to C Programming- Need help understanding !! operator
    By shellwoo3 in forum C Programming
    Replies: 4
    Last Post: 07-17-2012, 07:18 PM
  4. Replies: 3
    Last Post: 12-09-2008, 11:19 AM
  5. Replies: 2
    Last Post: 07-07-2008, 03:46 AM

Tags for this Thread