How to read bits of a byte with c

mihir83in's Avatar, Join Date: Jun 2007
Light Poster
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();
}
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
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.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
You have the &(And) |(Or) ^ (Xor) operations as well.
0
clocking's Avatar, Join Date: Jun 2007
Ambitious contributor
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!
0
mihir83in's Avatar, Join Date: Jun 2007
Light Poster
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
0
mihir83in's Avatar, Join Date: Jun 2007
Light Poster
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.
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
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.