# Bit Manipulation Program

#### Kittu20

Joined Oct 12, 2022
431
I've written C code for setting, clearing, and toggling individual bits in an integer

C:
#include<stdio.h>

void Bit_Set ( int num, int pos)
{
num = num | 1 << pos;
printf(" num = %d", num);
}

void Bit_Clear ( int num, int pos)
{
num = num & (~( 1 << pos));
printf(" num = %d", num);
}
void Bit_toggle ( int num, int pos)
{
num = num ^ 1 << pos;
printf(" num = %d", num);
}

int main()
{
//Bit_Set ( 0, 2);
//Bit_Clear ( 255, 1);
//Bit_toggle ( 255, 7);

return 0;
}
However, I now want to expand upon this and create a more versatile program that can set, clear, or toggle a specified number of bits within an integer value. To illustrate, let me provide you with an example:

Suppose the user inputs the following instructions:

• Set the higher two bits of the integer.
• Clear three bits from the upper next, higher- bytes of the integer.
• Toggle three bits in the lower-order bytes of the integer.

I would like to develop a program that can take such user-defined instructions and perform the corresponding bit manipulations on the integer value. looking your help

#### WBahn

Joined Mar 31, 2012
29,865
If your function to set a bit is supposed to set a bit, why is that function printing anything out?

When you call the sqrt() function, does it print something out?

Doesn't it return something so that you can actually use it?

Shouldn't your functions return something so that you can actually use it?

When you have the statement:

num = num | 1 << pos;

Do you know which operation is performed first? Are are you just hoping? Use parens to force the compiler to do things in the order you want and to make that order clear to the reader.

As for your more general case, you first need to decide and define how the user is going to indicate such complex and arbitrary desires.

Then, when you have proper bit manipulation functions that actually return values you can use, just use to functions to perform the desired operations.

#### nsaspook

Joined Aug 27, 2009
12,796
For embedded programming it's always good idea to see that the compiler is using at the ASM level for your source code with bit manipulations that are used often and possibly with interrupts. Many controllers have atomic (single instruction operation) bit-set and bit-clear instructions but that's a hardware specific optimization might be used if called the correct way using C bit operators.

ANSI-C expressions and statements should always be assumed to be non-atomic.

https://onlinedocs.microchip.com/pr...tml?GUID-507FE8EF-0C67-4C46-85BE-DC85AB5A56A4
Set, Clear and Read Register Bits

https://onlinedocs.microchip.com/pr...tml?GUID-9537F68C-E2C4-46BA-973C-5C12232F6B54
Application Example Showing Alternative Ways of Writing Code

#### Kittu20

Joined Oct 12, 2022
431
Do you know which operation is performed first? Are are you just hoping? Use parens to force the compiler to do things in the order you want and to make that order clear to the reader.

As for your more general case, you first need to decide and define how the user is going to indicate such complex and arbitrary desires.
I tried the following approach, but I encountered an issue. I can either set bits or clear bits, but I can't to perform both operations simultaneously.

C:
#include <stdio.h>

{
// num = num & (~clear_mask);
printf("%d", num);
}

int main()
{
int num = 5;

// Create a bitmask to set bits 1 and 3
int set_mask = (1 << 1) | (1 << 3);

// Create a bitmask to clear bits 0 and 2
int clear_mask = (1 << 0) | (1 << 2);

return 0;
}

#### WBahn

Joined Mar 31, 2012
29,865
I tried the following approach, but I encountered an issue. I can either set bits or clear bits, but I can't to perform both operations simultaneously.

C:
#include <stdio.h>

{
// num = num & (~clear_mask);
printf("%d", num);
}

int main()
{
int num = 5;

// Create a bitmask to set bits 1 and 3
int set_mask = (1 << 1) | (1 << 3);

// Create a bitmask to clear bits 0 and 2
int clear_mask = (1 << 0) | (1 << 2);

return 0;
}
What is it doing or not doing that you think is wrong?

Your original value is 5, so that's 0101b.

You want to set bits 1 and 3, so that becomes 1111b.

You want to clear bits 0 and 2, so that becomes 1010b.

You print that out as a decimal number, so that's going to be 10.

What is it printing out for you?

You need to ask yourself what you want to have happen if the user says that they want to set and clear the same bit.