Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Get one bit from byte using c

Status
Not open for further replies.

Micro Lover

Member level 2
Joined
Jul 22, 2009
Messages
44
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
pakistan
Activity points
1,614
hi

In "C" i have two Variables, one is 8-bit variable, one is 1-bit, like

unsigned char temp = 123;
bit DataSaved = 0;

now i want to copy anyone bit from "temp" to "DataSaved"

haw can i do it?
 


Code C - [expand]
1
2
3
4
if (temp & (1 << bitnumber)) 
   Datasaved = 1;
else
   Datasaved = 0;

 

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#define SETBIT(ADDRESS,BIT) (ADDRESS |= (1<<BIT)) 
#define CLEARBIT(ADDRESS,BIT) (ADDRESS &= ~(1<<BIT)) 
#define CHECKBIT(ADDRESS,BIT) (ADDRESS & (1<<BIT)) 
#define WRITEBIT(RADDRESS,RBIT,WADDRESS,WBIT) (CHECKBIT(RADDRESS,RBIT) ? SETBIT(WADDRESS,WBIT) : CLEARBIT(WADDRESS,WBIT))
 
// one way is
if CHECKBIT(temp,bitnumber) Datasaved = 1;
else Datasaved = 0;
 
// this is a secod way, it can probably work but I haven't tried with a 1bit variable
WRITEBIT(temp,bitnumber,Datasaved,0);
 
//if it doesn't work then use the following define instead
#define WRITEBT(RADDRESS,RBIT,WADDRESS) (CHECKBIT(RADDRESS,RBIT) ? (WADDRESS=1) : (WADDRESS=0)
WRITEBT(temp,bitnumber,Datasaved);




Alex
 
  • Like
Reactions: teophe

    teophe

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top