Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   How to read bits of a byte with c (http://www.go4expert.com/forums/read-bits-byte-c-t4651/)

mihir83in 13Jun2007 03:22

How to read bits of a byte with c
 
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();
}


DaWei 13Jun2007 03:44

Re: How to read bits of a byte with c
 
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.

shabbir 13Jun2007 08:57

Re: How to read bits of a byte with c
 
You have the &(And) |(Or) ^ (Xor) operations as well.

clocking 13Jun2007 18:46

Re: How to read bits of a byte with c
 
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!

mihir83in 16Jun2007 01:10

Re: How to read bits of a byte with c
 
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

mihir83in 18Jun2007 11:40

Re: How to read bits of a byte with c
 
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.

DaWei 18Jun2007 16:22

Re: How to read bits of a byte with c
 
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.


All times are GMT +5.5. The time now is 19:14.