Thread: advanced bit fields

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    58

    advanced bit fields

    here i have used a char * and have malloc ed an arbitrary number of bytes to it. now what i need to do is fill all the 5 bytes with 0(that is taken care of by the *ch=0 , no problem). now after this i or ch with 1 and shift it left 1 time. if u see the output at this time u will notice that not only is the 2nd bit (from right) is set to 1 but also another bit in the 5th byte. how do u correct this? or is such thing not possible. the code is like this.can be run in TC. thanx in advance.
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<alloc.h>
    
    void showbits(char *n,int b)
    {
    	int i,k;
    	int *mask;
    	mask=(int *)malloc(b);
    	int count=0;
    	for(i=(8*b)-1;i>=0;i--)
    	{
    		*mask=1<<i;
    		k=*n & *mask;
    		k==0 ? printf("0"):printf("1");
    		count++;
    	}
    	printf("\n%d\n",count);
    }
    
    int main()
    {
    //	clrscr();
    	char *ch;
    	ch=(char *)malloc(5);
    	*ch=0;
    	for(int i=0;i<8;i++)
    	{
    		*ch=*ch | 1;
    		*ch=*ch<<1;
    	}
     	*ch=*ch | 1;
    	*ch=*ch<<1;
    	showbits(ch,5);
    	getch();
    	return 0;
    }
    even a fish would not have been in danger had it kept its mouth shut.

  2. #2
    Registered User
    Join Date
    May 2004
    Posts
    127
    Interesting code. What output were you expecting opposed to what you actually got? I don't understand your intentions.

  3. #3
    Registered User penney's Avatar
    Join Date
    Jan 2003
    Posts
    47
    I am not sure of what you are trying to do. However, 1st off *ch = 0; will not set all 5 bytes to 0. I assume you are trying to set all bits in a char to 1 bit by bit. You could of course just assign 0xFF to the char but if it's for an exercise sake then ok. Anyway, I believe the code below is closer to what you are trying to do but I just modified your code so it's still ugly but it works.

    Code:
    void showbits(unsigned char *n,int b);
    
    int main(void)
    {
      int i,j;
      unsigned char *ch;
    
      ch=malloc(sizeof(unsigned char)*5);
      memset(ch,0,sizeof(unsigned char)*5);
      for(j=0;j<5;j++) /* Next byte */
      {
         /* Set 1st bit to 1 */
          *(ch+j)=*(ch+j) | 1;
          for(i=0;i<7;i++)
         {
             *(ch+j)=*(ch+j)<<1; /* Shift all bits left 1 */
             *(ch+j)=*(ch+j) | 1;  /* Reset 1st bit to 1 */
         }
      }
    }
    
    void showbits(unsigned char *n,int b)
    {
     int i,j,k;
     unsigned char mask;
     int count=0;
     for(j=b-1;j>0;j--)
     {
         mask = 128;
         for(i=7;i>=0;i--)
         {
             k=*(n+j) & mask>>i;
             k==0 ? printf("0"):printf("1");
             count++;
         }
     }
     printf("\n%d\n",count);
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    for(int i=0;i<8;i++)
    {
    	*ch=*ch | 1;
    	*ch=*ch<<1;
    }
    *ch=*ch | 1;
    *ch=*ch<<1;
    All this code does is set the value to 254. The last bit (1s) doesn't get set, because you shift at the end. The last two lines server no purpose at all, because that's already been done inside the loop. They're pointless.

    Furtheremore, there is no reason to malloc five bytes, because you only ever use one of them.

    Finally, your showbits function is well, bad. It doesn't show the bits the way they are. It ... It's wrong. It doesn't just show all set bits. If that is what it was supposed to do, it doesn't. While we're at it, you never free anything you malloc. Not to mention the fact that you use C99 variable declaration some of the time, but not all the time. The idea is to be consistant in what you do.

    Consider the following replacement:
    Code:
    void showbits( unsigned char *n, int b )
    {
            int i,j;
    
            for( j = 0; j < b; j++, n++ )
            {
                    for( i = 0; i < 8; i++ )
                            printf("%d", *n & (1<<i) ? 1 : 0 );
                    printf("\n");
            }
    }
    Which is what I assume you were attempting to do. I agree with Kip, it's "interesting".

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bit fields problem
    By johndirect in forum C Programming
    Replies: 2
    Last Post: 10-21-2008, 11:15 AM
  2. union in struct with bit fields
    By sagarnewase in forum C Programming
    Replies: 4
    Last Post: 05-12-2008, 07:30 AM
  3. bit patterns of negtive numbers?
    By chunlee in forum C Programming
    Replies: 4
    Last Post: 11-08-2004, 08:20 AM
  4. Create data type
    By Malefaust in forum C Programming
    Replies: 2
    Last Post: 09-30-2004, 06:06 AM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM