We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,140 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

bit manipulation in c

Hi

we have a buffer say char array.[1].so the size will be 1 byte.
Now our problem is say we have 8 bits data say 10110101.
now this must be assigned to array[0] variable as 1 byte.
bit manipulation is not happening.kindly help us in this regard
If possible post us a sample code for the same

here we are posting sample code for the same

decleration:
struct aaa {
unsigned char buff1:1;

unsigned char buff2:1;

unsigned char buff3:1;

unsigned char buff4:1;

unsigned char buff5:1;

unsigned char buff6:1;

unsigned char buff7:1;

unsigned char buff8:1;
}complete_buff;
                               complete_buff->buf1=(unsigned char)0x01;;
				complete_buff->buf2=(unsigned char)0X00;
				complete_buff->buff3=(unsigned char)0x01;
				complete_buff->buff4=(unsigned char)0x01;
				complete_buff->buff5=(unsigned char)0x00;
				complete_buff->buff6=(unsigned char)0x00;
				complete_buff->buff7=(unsigned char)0x00;
				complete_buff->buff8=(unsigned char)0x00;

                               char  array[0]=(complete_buff->buff1>>7)|                (complete_buff->buff2>>6)|(complete_buff->buff3>>5)|(complete_buff->buff4>>4)|(complete_buff->buff5>>3)|(complete_buff->buff6>>2)|(complete_buff->buff7>>1)|(complete_buff->buff8)
4
Contributors
3
Replies
8 Hours
Discussion Span
3 Years Ago
Last Updated
5
Views
deeep
Newbie Poster
12 posts since Oct 2009
Reputation Points: 16
Solved Threads: 1
Skill Endorsements: 0

Get rid of the shifts, you don't need them since you are using a bit field and are assigning directly to the various bits. Then, change your current assignments:

complete_buff->buf1=(unsigned char)0x01;;
complete_buff->buf2=(unsigned char)0X00;
complete_buff->buff3=(unsigned char)0x01;
complete_buff->buff4=(unsigned char)0x01;
complete_buff->buff5=(unsigned char)0x00;
complete_buff->buff6=(unsigned char)0x00;
complete_buff->buff7=(unsigned char)0x00;
complete_buff->buff8=(unsigned char)0x00;

To this:

complete_buff.buf1=(unsigned char)0x01;;
complete_buff.buf2=(unsigned char)0X00;
complete_buff.buff3=(unsigned char)0x01;
complete_buff.buff4=(unsigned char)0x01;
complete_buff.buff5=(unsigned char)0x00;
complete_buff.buff6=(unsigned char)0x00;
complete_buff.buff7=(unsigned char)0x00;
complete_buff.buff8=(unsigned char)0x00;

You only use the "arrow" operator with pointers. Since you don't have a pointer, you use the "dot" operator.

Fbody
Posting Maven
2,929 posts since Oct 2009
Reputation Points: 833
Solved Threads: 394
Skill Endorsements: 5

Hi

we have a buffer say char array.[1].so the size will be 1 byte.

Why? An array is multiple bytes. 1 byte is a byte, not an array.

I have a fleet of cars -- it's a Ford.
I have a pride of lions -- his name is Leo
I have a pair of socks -- where's the other pair for my left foot.

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37

I have a fleet of cars -- it's a Ford.
I have a pride of lions -- his name is Leo

http://icanhascheezburger.files.wordpress.com/2007/05/jesus_christ_its_a_lion.jpg

jephthah
Posting Maven
2,592 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
Skill Endorsements: 5

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0697 seconds using 2.73MB