Important alert: (current site time 7/25/2014 4:39:19 AM EDT)
 

VB icon

Get a bit, extract a value, or set a bit from a number

Email
Submitted on: 6/6/2000 10:35:06 PM
By: Mike A. Leonetti  
Level: Intermediate
User Rating: By 3 Users
Compatibility: C, C++ (general)
Views: 17909
author picture
(About the author)
 
     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?
Click here for a copy-and-paste friendly version of this code!
//**************************************
//INCLUDE files for :Get a bit, extract a value, or set a bit from a number
//**************************************
#include <iostream.h>
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// 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
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=485&lngWId=3//for details.//**************************************

/*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";
	}


Other 5 submission(s) by this author

 


Report Bad Submission
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:

Your Vote

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 ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.