Solved

Hov do i get bit in integer?

Posted on 2003-11-04
2
1,489 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Where is this file? 3 42
Please explain purpose of GZIP 4 81
Why use this lambda? 12 94
asp.net web app does not update text field to show progress during long process 8 27
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
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
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Suggested Courses

738 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