Thread: Set, Clear and Toggle a specific bit

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    68

    Set, Clear and Toggle a specific bit

    Are all my operations correct if I want to Set, Clear and Toggle a 7 bit
    Code:
    clear 7th bit 
    
    1010 1001   byte
    0000 0000
    ----------  AND logic 
    1000 0000
    
    
    set 7th bit 
    
    
    0101 1001   byte
    1000 0000
    ----------  OR logic 
    1000 0000
    
    
    Toggle 7 bit 
    0101 1001   byte
    1000 0000
    ----------  xOR logic 
    1010 0110

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    673
    Nope:

    Code:
    #define clear_bit( v, bit ) ( (v) & ~(1U << (bit)) )
    #define set_bit( v, bit ) ( (v) | (1U << (bit)) )
    #define toggle_bit( v, bit ) ( (v) ^ (1U << (bit)) )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Toggle Hardware acceleration vb.net, C#
    By init3d in forum Windows Programming
    Replies: 4
    Last Post: 07-31-2013, 06:48 AM
  2. Toggle a particular bit in a variable
    By ArunS in forum C Programming
    Replies: 2
    Last Post: 01-04-2012, 12:46 AM
  3. toggle bit in unsigned long var
    By thescratchy in forum C++ Programming
    Replies: 7
    Last Post: 02-19-2011, 11:19 AM
  4. toggle switches and LEDs
    By cprogrammer2003 in forum C Programming
    Replies: 11
    Last Post: 07-22-2003, 12:35 PM
  5. more specific clear screen
    By UniqueuserName in forum C++ Programming
    Replies: 2
    Last Post: 12-10-2002, 11:38 AM

Tags for this Thread