Setting bits

This is a discussion on Setting bits within the C Programming forums, part of the General Programming Boards category; How would I set xth bit in a int j? All i can think of is something like this: #define ...

  1. #1
    zsaniK Kinasz's Avatar
    Join Date
    Jan 2003
    Posts
    222

    Setting bits

    How would I set xth bit in a int j?

    All i can think of is something like this:


    #define FIRST 0x00000001
    #define SECOND 0x00000010
    #define THIRD 0x00000100
    #define FOURTH 0x00001000

    #define BIT_TO_SET 4

    int j = 0;

    switch ( BIT_TO_SET ) {
    case 1: j |= FIRST;
    break;
    case 2: j |= SECOND;
    break;
    case 3: j |= THIRD;
    break;
    case 4: j |= FOURTH;
    case etc...
    }



    Is there a better or more effiecent way to set individual bits based on a int? This is for a PIC so every cycle counts and what I've got just seems a little clunky.

    Cheers
    "Assumptions are the mother of all **** ups!"

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Code:
    dec         bin
     1             1
     2             10
     3             11
     4             100
     ...
     8             1000
    See a pattern? You can OR it with 2^n to set the nth bit
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    zsaniK Kinasz's Avatar
    Join Date
    Jan 2003
    Posts
    222
    can you give an example?

    if i wanted to:

    2 ^ n
    2 ^ 3 // assume n is 3 so we can set the third bit
    10 ^ 11 // binary
    01 // result


    that would set the first bit, I dont think I am understanding
    Last edited by Kinasz; 07-04-2004 at 10:12 PM.
    "Assumptions are the mother of all **** ups!"

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Actually theres probably a better way...I'm thinking bitshift:
    Code:
    int SetBit(int j, int bit)
    {
    return  j|=(1<<bit);
    }
    Something like that anyways...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Maybe I should explain better because I'm not doing good at helping you so far heh.

    That would simply OR whatever number you have with whatever bit you want to set (starting at zero, keep in mind)

    The bitshift moves the number one digit, in binary, to the left
    So 1<<0 = 1 (1 in binary)
    1<<1 = 2 (10 in binary)
    1<<2 = 4 (100 in binary)
    1<<3 = 8 (1000 in binary)

    You can also use shift right (>>) similarly

    If you don't know how to convert between decimal and binary (and other bases for that matter) now is a good time to learn
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    ---
    Join Date
    May 2004
    Posts
    1,379
    oh how i love bit shifting

  7. #7
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    You may also want to check out bitfields

    Code:
    #define ON  1
    #define OFF 0
    
    typedef struct _Flags
    {
        unsigned int bit1 : 1;
        unsigned int bit2 : 1;
        unsigned int bit3 : 1;
      
        unsigned int pad : 5;
    } Flags;
    
    
    Flags f;
    f.bit1 = ON;
    Not normal practice, but a nifty idea I thought up, if I do say so myself =). Of course, I wouldn't normally do something like this, I'd #define the value of single bits before using this.
    Last edited by skorman00; 07-04-2004 at 11:38 PM.

  8. #8
    zsaniK Kinasz's Avatar
    Join Date
    Jan 2003
    Posts
    222
    ah, i see

    Thanx heaps dude that was a really helpful explanation and I have achieved what I wanted!!
    "Assumptions are the mother of all **** ups!"

  9. #9
    zsaniK Kinasz's Avatar
    Join Date
    Jan 2003
    Posts
    222
    Just looked at bitfields, that may come in handy when the prog gets a bit bigger, thanks
    "Assumptions are the mother of all **** ups!"

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Perhaps a few handy macros are in order:
    Code:
    #define bit(i)      (1UL << (i))
    #define set(x, i)   (x |= bit(i))
    #define clear(x, i) (x &= ~bit(i))
    #define test(x, i)  (x & bit(i))
    This saves you from having to remember how to do what and avoids somewhat ugly code.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. setting the values of bits
    By yahn in forum C++ Programming
    Replies: 14
    Last Post: 04-14-2008, 01:09 AM
  2. SDLKey to ASCII without unicode support?
    By zacs7 in forum Game Programming
    Replies: 6
    Last Post: 10-07-2007, 04:03 AM
  3. Help counting number of bits set in an integer
    By JayDiddums10 in forum C Programming
    Replies: 5
    Last Post: 12-07-2006, 03:21 PM
  4. Setting bits
    By Buckshot in forum C++ Programming
    Replies: 15
    Last Post: 06-06-2005, 01:37 PM
  5. Writing binary data to a file (bits).
    By OOPboredom in forum C Programming
    Replies: 2
    Last Post: 04-05-2004, 04:53 PM

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