Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
C programming example implementing a function to set a specified bit in an int (from ECE 2534)
// Example program that implements the function setBit(x, i) // that returns the value of x with the bit at index i set to a 1, // where 0 <= i <= 31. All other bits in x should be left unchanged. // ----------------------------------------------------------------- // To run the program in rextester, you will need to enter the // bit number and the integer value to test as a hex number in the input window // That is, type something into the "Show input" window, e.g., "6 0xFFFF0000" #include <stdio.h> // horrible recursive helper function to print int as binary void printBinary(unsigned int number) { if (number) { printBinary(number >> 1); putc((number & 1) ? '1' : '0', stdout); } } // my setBit function int setBit(int x, int i) { int returnValue; int bitMask; // set bit mask bitMask = (1 << i); // set the bit from the bit mask using a bitwise OR returnValue = x | bitMask; return(returnValue); } int main(void) { unsigned int initialValue, returnedValue; int n; printf("Remember to input the bit number to set and the hex number in the input window!\n\n"); // read in the value as a hex number scanf("%d %x\n", &n, &initialValue); // print out bit that will be cleared printf("Bit to be set is %d\n",n); // print out the value read in as a binary number printf("Input: "); printBinary(initialValue); // set bit n returnedValue = setBit(initialValue, n); // print out the returned value as a binary number printf("\nOutput: "); printBinary(returnedValue); return 0; }
run
|
edit
|
history
|
help
0
Time Stamp
Goodone tricky
Matrix rotation 90-degree
linear hybrid cellular automaton reversible random bit generator stream cipher
convertletter
Project 3 Part 1 v1.4
Max of arrays
150112_CribaErastotenes
C_141204_ConcatenarCadenas
SUMALE UNO