Thread: Logic to set more than one bits

  1. #1
    Registered User
    Join Date
    Oct 2022
    Posts
    98

    Logic to set more than one bits

    I wanted to discuss a situation where we need to set more than two bits in a 8-bit or 16-bit or 32-bit register and explore a suitable logic

    By using below approach, we can set multiple bits in a 8 -bit register

    Code:
     #include<stdio.h>
    
    void set_Bits ( char num, char set_mask)
    {
    	num = num | set_mask;           // 00000010 OR 00000101 = 0000 0111 = 7
    	
    	printf("num : %d \n", num);     // Print value 7
    	
    }
    
    
    int main()
    {
    	unsigned char num = 2; // binary 0000 0010  [MSB7 -LSB0] 
    	unsigned char set_mask; 
    	set_mask  = (1 << 0)| (1 << 2);  // 00000001 OR 00000100 = 00000101
        printf("mask value %d \n", set_mask);
    	set_Bits( num, set_mask );
    	return 0;
    }
    This logic is adaptable to different situations like 16 bit or 32 bit

    but its matter of suitable suggest good way How to set 16 bits in 32 bit register

    Please let me know your thoughts on this proposed logic, if you want to offer any efficent logic

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    957
    C doesn't really have the concept of "registers". You can declare variables of specific sizes, though, with types from stdint.h:

    • int8_t/uint8_t: signed/unsigned 8 bits
    • int16_t/uint16_t: signed/unsigned 16 bits
    • int32_t/uint32_t: signed/unsigned 32 bits


    The logic to set bits is the same, regardless of size. But if you're left-shifting a 1 to make a mask, cast the 1 to the appropriate type first, as in (uint32_t)1 << 18, otherwise it may not work on systems where int is smaller than the type you're using (smaller/older/embedded systems will often have 16-bit int).

    Edit: yes, C does have a register keyword, but that's a hint to the compiler to keep a variable in a register. Other than that, the C language doesn't know what registers are.

  3. #3
    Registered User
    Join Date
    Oct 2022
    Posts
    98
    I'd like to clarify that my question about setting multiple bits is within the context of embedded systems, where we can use 8-bit, 16-bit, or 32-bit microcontrollers.

    I've demonstrated how I would do it for one-byte variables in an 8-bit microcontroller. Would you recommend any alternative methods for 32-bit microcontrollers?

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    957
    The basic method doesn't change, whether you're on an 8-bit, 16-bit, or 32-bit microcontroller. Why do you think your code wouldn't work on a 32-bit or even a 64-bit CPU?

    Setting bits is just about the simplest thing you can do, and there's not many "better" ways to do it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Changing particular bits and detecting toggled bits
    By Nordomus in forum C++ Programming
    Replies: 11
    Last Post: 04-13-2018, 12:07 PM
  2. Extracting certain bits from sequence of bits
    By lucaspewkas in forum C Programming
    Replies: 5
    Last Post: 10-06-2007, 12:22 AM
  3. New idea on conveting byte to bits/bits to byte
    By megablue in forum C Programming
    Replies: 10
    Last Post: 10-26-2003, 01:16 AM
  4. copy some bits into a 8 bits binary number
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 05-29-2002, 10:54 AM
  5. Help Please...bits
    By Unregistered in forum C Programming
    Replies: 11
    Last Post: 01-24-2002, 01:43 PM

Tags for this Thread