how to set bit X of Y to 1?

This is a discussion on how to set bit X of Y to 1? within the C++ Programming forums, part of the General Programming Boards category; I think x&=~(1<<y) would set bit # y to 0 but I cant figure out how to set it to ...

  1. #1
    evader
    Guest

    Question how to set bit X of Y to 1?

    I think x&=~(1<<y) would set bit # y to 0 but I cant figure out how to set it to 1. I tried things like x&=(1<<y) but it sets all of x to 1 and not only the bit that I need.
    Anyone know the correct algorith?

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Try x|=(1<<y).
    zen

  3. #3
    evader
    Guest

    Exclamation

    Thanks, it works. I was trying to fix that problem for 15 hours a day for 3 days and I was loosing all hope for making my program work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bit operation question
    By mr_coffee in forum C Programming
    Replies: 3
    Last Post: 04-07-2009, 06:00 PM
  2. set a flag in a bit stream
    By sarathius in forum C Programming
    Replies: 2
    Last Post: 02-06-2008, 04:25 AM
  3. Find all possible subset of a given set
    By Downnin in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 02:03 PM
  4. "if you love someone" :D
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-02-2003, 02:10 AM
  5. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 05:10 AM

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