In: Computer Science
Write an assembly language program code to clear and set bit 7th
and 19th in a 32-bit variable
called N.
Ans: Assembly Language Program in this you can set, clear and toggle any bit variable.
// C program to set, clear and toggle a bit
#include <stdio.h>
// Function to set the kth bit of n
int setBit(int n, int k)
{
return (n | (1 << (k - 1)));
}
// Function to clear the kth bit of n
int clearBit(int n, int k)
{
return (n & (~(1 << (k - 1))));
}
// Function to toggle the kth bit of n
int toggleBit(int n, int k)
{
return (n ^ (1 << (k - 1)));
}
// Main Logic
int main()
{
int n = 5, k = 1;
printf("%d with %d-th bit Set: %d\n",
n, k, setBit(n, k));
printf("%d with %d-th bit Cleared: %d\n",
n, k, clearBit(n, k));
printf("%d with %d-th bit Toggled: %d\n",
n, k, toggleBit(n, k));
return 0;
}