How to read bits of a byte with c

Light Poster
13Jun2007,03:22   #1
mihir83in's Avatar
Greetings. My goal is to obtain individual bits within a byte with C. The reason is to develope and experiments with some encryption schemes. Now I know that we can read a bytesize from fread and fwrite, but i want the individual bits of it, one option would be to use the >> and << bitwise operators with a mask bit pattern or a modulation operator, but that is quite inefficient i think when dealing with large files. I would be obliged if anybody can guide to any alternative approach to this problem. Thanks in advance.

here is the code i have used for getting bits out of a byte, currently its for a character inputted through keyboard, but it can be adopted with any I/O by converting it the other way around, but as i said i think there must be some more proficient method of doing it.

Code:
#include<stdio.h>
#include<conio.h>

void main()
{
	int bits[8];
	int b;
	int cnt=7;
	char ch;
	clrscr();
	printf(" Enter a value or character ");
	scanf("%c",&ch);
	clrscr();
	while(cnt>=0)
	{
		b=ch%2;
		bits[cnt]=b;
		a=a/2;
		cnt--;
	}


	for(cnt=0;cnt<8;c++)
		printf("%d",bits[cnt]);
	getch();
}
Team Leader
13Jun2007,03:44   #2
DaWei's Avatar
You can't read less than a byte from a file. If you could, it would still be terribly inefficient because file operations are orders of magnitude slower than memory operations. You'll need to stick with shifts, masks, or modulus. Modulus is generally slower than shifts, but your compiler will usually optimize it to a shift, where that would be faster.

You CAN read more than one byte, using fread. It doesn't really gain you anything but a larger number of bits in a larger variable. In any reasonably modern system, the system will read an entire sector, minimum, at a time. It may read much more, if it has available cache. This will then be parceled out to your program, where your language's IO stuff will further adapt it to the type of read you asked for.
Go4Expert Founder
13Jun2007,08:57   #3
shabbir's Avatar
You have the &(And) |(Or) ^ (Xor) operations as well.
Ambitious contributor
13Jun2007,18:46   #4
clocking's Avatar
Quote:
Originally Posted by DaWei
You can't read less than a byte from a file. If you could, it would still be terribly inefficient because file operations are orders of magnitude slower than memory operations. You'll need to stick with shifts, masks, or modulus. Modulus is generally slower than shifts, but your compiler will usually optimize it to a shift, where that would be faster.

You CAN read more than one byte, using fread. It doesn't really gain you anything but a larger number of bits in a larger variable. In any reasonably modern system, the system will read an entire sector, minimum, at a time. It may read much more, if it has available cache. This will then be parceled out to your program, where your language's IO stuff will further adapt it to the type of read you asked for.
ok!
I'm a student. And I love programming so much.
I think, you can expert to me.
If you agree, please send to me at: thuytrieudang04@yahoo.com
thanks so much!
Light Poster
16Jun2007,01:10   #5
mihir83in's Avatar
Sorry for the late reply.
I was looking for a better approach to the problem rather than bit masking and bitwise operator.

I am finding it , as soon as i find it I will post it.
Thank you shabbir and dawei
Light Poster
18Jun2007,11:40   #6
mihir83in's Avatar
Well Greetings ,
I have found the solution to my problem of reading bits from a byte, so i am posting it here. The solution is the fread() and fwrite() Functions. we can read one byte at a time from them. If we store this byte in a bit structure with 8 bitfields. Then we can use the '.' operator and refer to individual bits in a byte. This way there is no need for modulation or masking of bits,

Regards.
Team Leader
18Jun2007,16:22   #7
DaWei's Avatar
Well, that's easier to use, yes, but if you examine the emitted code you will see that the machine is using shifts, masks, and bit operators. You won't find a real performance gain.