Solved

Hov do i get bit in integer?

Posted on 2003-11-04
2
1,468 Views
Last Modified: 2008-02-01
Hello

How do I get a specific bit in an integer?

let's say i have the value 123 in an int. That's 1111011 in binary.

How do i get the walue of the the 3rd bit (ie 0) or the 4th bit (ie 1)???
0
Comment
Question by:sk-man
2 Comments
 
LVL 20

Accepted Solution

by:
TheAvenger earned 125 total points
ID: 9677710
Note that this code counts the bits starting from 0, not from 1, so the bits you give as an example are 2 and 3, not 3 and 4

int val = 123;
int bitToTake = 2;

// As a boolean: set/not set
bool bitSet = (val >> bitToTake & 0x01) == 0x01;

// As a number: 1/0
int bit = val >> bitToTake & 0x01;
0
 

Author Comment

by:sk-man
ID: 9686383
Thank you.

It really helped me a lot!
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.NET (2008) - Refactoring Question 2 21
Printing 1 51
Please explain the parts of these 2 LINQ expressions 3 30
insert value of checklistbox checked 4 27
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question