Thread: Changing a single bit of an integer

  1. #1
    Registered User Tiago's Avatar
    Join Date
    Oct 2009
    Location
    Lisbon, Portugal
    Posts
    28

    Changing a single bit of an integer

    I've created a 4 bits Int... I want to change only one of the 4 bits of this Int, how do I do that?

    ty...

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Using the bit operators of |, &, or ^, depending on what it is you are trying to do.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Uhhmmm. I'm assuming you are confusing a bit with a byte here?

  4. #4
    Registered User Tiago's Avatar
    Join Date
    Oct 2009
    Location
    Lisbon, Portugal
    Posts
    28
    Quote Originally Posted by claudiu View Post
    Uhhmmm. I'm assuming you are confusing a bit with a byte here?
    actually I'm not

    Code:
    unsigned int type:4;

    Quote Originally Posted by tabstop View Post
    Using the bit operators of |, &, or ^, depending on what it is you are trying to do.
    I'm not seeing how I'm going to change a single bit from the bit-word without adding too many logic to the program (I hate logic, even more if it's 1 AM), instead I'm going to use four 1-bit words... ty
    Last edited by Tiago; 04-17-2010 at 05:52 PM.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you never need the value as a single coherent thing then that will work marvelously. (I'm not sure how using an operator is "too much logic", but you've certainly got the edge in readability.)

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Tiago View Post
    Code:
    unsigned int type:4;
    I'm not seeing how I'm going to change a single bit from the bit-word without adding too many logic to the program (I hate logic, even more if it's 1 AM), instead I'm going to use four 1-bit words... ty
    Well, logic is the only practical choice you have. Single upset events, while having the ability to change the value of single bits regardless of your program logic, are generally considered a little too unpredictable for practical application.

    You will need to copy your 4 bit int into a normal unsigned variable, manipulate the 4 bits of that variable, and copy the result back into your 4 bits.

    Bitfields, such as you are using, are mostly used to represent smaller value ranges (eg a 4 bit unsigned bitfield can represent values in the range 0 to 15), not for playing with individual bits.

    Keep in mind that the organisation of bitfields within a type are implementation defined.
    Last edited by grumpy; 04-17-2010 at 06:15 PM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    The SNIPPETS C collection - file:

    All you need mate; thinking not required but you might find thought useful if you get into programming on any serious level...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    This is very common when doing embedded programming. Many systems define macros of all the pins as the position from the right, so you can use shifts to single out a single bit. For example, if you want to set a specific bit in a register:

    Code:
    REGISTER |= (1 << PIN_POSITION)
    Or if you want to reset the bit:
    Code:
    REGISTER &= ~(1 << PIN_POSITION)

  9. #9
    Registered User Tiago's Avatar
    Join Date
    Oct 2009
    Location
    Lisbon, Portugal
    Posts
    28
    Ok, thank you all...

    I've never been taught how to use bit operations and logic in C, but despite that (I'm going to have to learn it sooner or later), I'm going to spend some time trying to use this in my program, it doesn't seem that difficult, also.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bit value generation doubt
    By roaan in forum C Programming
    Replies: 2
    Last Post: 08-07-2009, 02:23 PM
  2. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  3. bit patterns of negtive numbers?
    By chunlee in forum C Programming
    Replies: 4
    Last Post: 11-08-2004, 08:20 AM
  4. newbie programmer - needs help bad.
    By hortonheat in forum C Programming
    Replies: 17
    Last Post: 10-20-2004, 05:31 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM