How to read bits of a byte with c

Discussion in 'C' started by mihir83in, Jun 12, 2007.

  1. mihir83in

    mihir83in New Member

    Joined:
    Jun 10, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    gujarat
    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();
    }
    
     
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    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.
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    You have the &(And) |(Or) ^ (Xor) operations as well.
     
  4. clocking

    clocking New Member

    Joined:
    Jun 12, 2007
    Messages:
    122
    Likes Received:
    0
    Trophy Points:
    0
    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!
     
  5. mihir83in

    mihir83in New Member

    Joined:
    Jun 10, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    gujarat
    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
     
  6. mihir83in

    mihir83in New Member

    Joined:
    Jun 10, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    gujarat
    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.
     
  7. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice