Bit check using function check(var,pos) ((var)&(1<<pos))

Discussion in 'C Programming' started by prati, Oct 27, 2012.

  1. prati

    prati

    Joined:
    Aug 2, 2012
    Messages:
    7
    I have written one small program to check whether particular bit is set or not.


    #define check(var,pos) ((var)&(1<<pos))
    void main()
    {

    int var=30;// in binary 0011 0000 that is its 4th and 5th bits are set;
    int bit;
    printf("enter position where we want to check whether bit is set or not ");
    scanf("%d",&pos);
    bit=check(var,pos);
    printf("%d",bit);
    getch();

    }


    output: when entered position
    0th(1<<0=0000 0001), bit value is 0
    1st(1<<1=0000 0010), bit value is 2
    2nd(1<<2=0000 0100), bit value is 4
    3rd1<<3=0000 1000), bit value is 8
    4th (1<<4 =0001 0000) , bit value is 16
    and when entered position is 5th(1<<5=0010 0000), bit value is 0.
    please let me know why it is so?what check(var,pos) returns?
    Last edited: Oct 27, 2012
    prati, Oct 27, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. threeinchnail@gmail.com

    Who can explain the bit'pos for me?

    threeinchnail@gmail.com, Jun 28, 2006, in forum: VHDL
    Replies:
    6
    Views:
    4,719
    D Stanford
    Jun 29, 2006
  2. markryde@gmail.com
    Replies:
    3
    Views:
    1,750
    Timothy Bendfelt
    Jan 19, 2007
  3. benn686@hotmail.com
    Replies:
    9
    Views:
    969
    Juha Nieminen
    Aug 22, 2007
  4. John W. Long

    perl pos function equivalent in ruby?

    John W. Long, Apr 8, 2004, in forum: Ruby
    Replies:
    4
    Views:
    179
    Robert Klemme
    Apr 13, 2004
  5. Jeff.M
    Replies:
    6
    Views:
    172
    Lasse Reichstein Nielsen
    May 4, 2009
Loading...

Share This Page