Solved

Macro definition to write a program to test a bit.

Posted on 2003-12-09
5
277 Views
Last Modified: 2010-04-15


how do you write a macro to test whether a bit in a byte is set or not set?

For example if we want to test the fourth bit of a byte, how do we do it using the macro.

0
Comment
Question by:suresh204
[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
5 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9909926
Hi suresh204,

#define ISSET4(X) (X)&(0x8)

Cheers!
Sunny:o)
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9909940
a small explanation is in order ...

given any byte say
                          1 1 0 1   1 0 1 0
to determine if nth bit is 1, bitwise AND it with a number which has all bits except nth bit as 0 ... here to determine, 4th bit we use
                          0 0 0 0   1 0 0 0

                          1 1 0 1   1 0 1 0
                     &   0 0 0 0   1 0 0 0
----------------------------------------------------
                          0 0 0 0    1 0 0 0
since answer is non zero, fourth bit is set

this is also referred to as masking .. since in the constant used, all other bits are set to 0 and 0 anded with anything gives a zero, the result depends on the only set bit in the constant (mask)
0
 
LVL 8

Accepted Solution

by:
ssnkumar earned 50 total points
ID: 9910136
You can use the following macro also:
#define IS_BIT_SET(X, pos) (0x01 << (pos - 1)) & (X)

"pos" is the bit which you want to test for number "X". Count starts from right. So rightmost bit is 1, next is 2, etc.
0

Featured Post

Independent Software Vendors: 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
rhel6 C system() call to zip to archive has problems 25 255
Finding a good hash function 4 132
Need example 5 137
Bitwise and to sum elements 2 54
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

752 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