 # Get a bit, extract a value, or set a bit from a number Email
 Submitted on: 1/1/2015 8:05:00 AM By: Mike A. Leonetti (from psc cd) Level: Intermediate User Rating:    By 3 Users Compatibility: C, C++ (general) Views: 6038

These two macros will get/modify the bit in a number according to a position counted from right to left. For example, let's say we had 1101 1111 (223). We would just use my macro to get the 3rd bit from the right which would be a one. We can also modify it to a 0. Or we want to extract the value of the first four bits, all can be done easily! ### INCLUDE files:

Can't Copy and Paste this?
 ```//************************************** //INCLUDE files for :Get a bit, extract a value, or set a bit from a number //************************************** #include ```
code:
Can't Copy and Paste this?
 ``` //************************************** // Name: Get a bit, extract a value, or set a bit from a number // Description:These two macros will get/modify the bit in a number according to a position counted from right to left. For example, let's say we had 1101 1111 (223). We would just use my macro to get the 3rd bit from the right which would be a one. We can also modify it to a 0. Or we want to extract the value of the first four bits, all can be done easily! // By: Mike A. Leonetti (from psc cd) //************************************** /*Bit Getting/Setting*/ //Macros// #define EXTRACT_BITS_RL(the_val, bits_start, bits_len) ((the_val >> (bits_start - 1)) & ((1 << bits_len) - 1)) #define MODIFY_BIT_RL(the_val, bit_num, bit_val) (bit_val == 0 ? (the_val & (~(1 << (bit_num - 1)))) : (the_val | (1 << (bit_num - 1)))) //End Macros// //Globals// int iVal = 0, iShift = 0, iBitVal = 0, iMenu = 1; //End Globals// //Prototyes// void ShowVal(); void ShowMenu(); //End Prototypes// int main() { while(iMenu < 4) //Better way of avoiding GOTOs { if(iMenu == 1) { cout << "Input Test Value (0 to 255): "; cin >> iVal; } if(iMenu == 0) { cout << "Enter Bit Position To Be Modified (1 to 8): "; cin >> iShift; cout << "Enter New Bit Value (0 or 1): "; cin >> iBitVal; iVal = MODIFY_BIT_RL(iVal, iShift, iBitVal); //Modify the number's bits } if(iMenu == 2) { ShowVal(); } if(iMenu == 3) { cout << "Enter Start: "; cin >> iShift; cout << "Enter Length: "; cin >> iBitVal; cout << "Extracted Value: " << EXTRACT_BITS_RL(iVal, iShift, iBitVal) << "\n"; } ShowMenu(); } return(0); } void ShowVal() { cout << "Binary Values (Right To Left) for \"" << iVal << "\"\n"; for(int I = 1; I < 9; I++) { cout << I << ": " << EXTRACT_BITS_RL(iVal, I, 1) << "\n"; //Show bit position (Right to Left) } } void ShowMenu() { //Display menu cout << "\n->Program Menu:\n0: Modify \"" << iVal << "\"\n1: Choose New Number\n2: Show Values for \"" << iVal << "\"\n3: Extract Value\n4: Quit\nChoose: "; cin >> iMenu; cout << "\n"; }```

Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)