1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

what is bit field?

Discussion in 'C' started by neha, Nov 18, 2006.

  1. neha

    neha New Member

    Joined:
    Nov 18, 2006
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    student in bca
    Location:
    delhi
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,292
    Likes Received:
    365
    Trophy Points:
    83
  3. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    A bit field is a common idiom used in computer programming to store a set of Boolean datatype flags compactly, as a series of bits. The bit field is stored in an integral type of known, fixed bit-width. Each Boolean flag is stored in a separate bit. Usually the source code will define a set of constants, each a power of two, that semantically associate each individual bit with its respective Boolean flag. The bitwise operators AND, OR, and NOT are used in combination to set, reset and test the flags.

    A bit field is distinguished from a bit array in that the latter is used to store a large set of bits indexed by integers and is often wider than any integral type supported by the language. Bit fields, on the other hand, typically fit within a machine word, and the denotation of bits is independent of their numerical index.
     
  4. ranims

    ranims New Member

    Joined:
    May 24, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    i am storing a number in long int, i.e.it is occuping 4 bytes.i am reading each byte data into a character array seperately. now by using those characters,how to to get the original value .
    Code:
    ex: number :3456789 
    Binary format is: 11 0100 1011 1111 0001 0101
    c[0]=0000 0000=    0
    c[1]=0011 0100=  52
    c[2]=1011 1111= 191
    c[3]=0001 0101=   21
    by using above chars how to get original value i.e. 3456789
     
    Last edited by a moderator: Jun 26, 2007
  5. 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:
    You have to be aware of the endianness of your particular implementation.
     
  6. ranims

    ranims New Member

    Joined:
    May 24, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Thanks a lot

    How to read the data from that string
    Code:
    main()
    {
    	long int li,num;
    	unsigned char c[4],temp;
    	int i,s;
    	clrscr();
    	printf("\nEnter any number: ");
    	scanf("%ld",&li);
    	printf("\nByte1 : %ld",li&0xFF);
    	c[0]=(char)li&0xFF;
    	
    	printf("\nByte2 : %ld",(li>>8)&0xFF);
    	c[1]=(char)(li>>8)&0xFF;
    	
    	printf("\nByte3 : %ld",(li>>16)&0xFF);
    	c[2]=(char)(li>>16)&0xFF;
    	
                    printf("\nByte4 : %ld",(li>>24)&0xFF);
    	c[3]=(char)(li>>24)&0xFF;
    	
                    for(i=0;i<4;i++)
    	{
    	    printf("\nactual: %d",c[i]);
    
    	}
    	s=2;
    	for(i=0;i<s;i++)
    	{
    		temp=0;
    		temp=c[i];
    		c[i]=c[4-i-1];
    		c[4-i-1]=temp;
    	}
    	for(i=0;i<4;i++)
    	{
    		printf("\nactual1: %d",c[i]);
    
    	}
    	
    	//printf("\noriginal: %ld",((c[0]>>24)&0x000000FF) |((c[1]>>8)&0x00FF)|((c[2]<<8)  &0xFF00)|((c[3]<<24)&0xFF000000));
    
    
    	 getch();
    }
    
    My requirement is by reading character array i should get original value which i have splitted
     
  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:
    There is no way to comment on your code without discussing endianness, as I mentioned. The bytes of the integer will be stored in one order on a Intel machine, another order on a Motorola machine, and perhaps even another order on another machine.

    Consider, for example, the number, 4660. In binary that would be (expressed with the most significant bit to the left) 0001 0010 0011 0100. In hex, it would be 0x1234. If you store that number at address 0 on an Intel machine, the contents of locations 0-3 will be, in hex, 34 12 00 00.

    On a 32-bit Motorola machine that would be 00 00 12 34. In other words, the Intel machine stores the "little end" first. This is called little endian. The Motorola machine is big endian. It stores the big (most significant) end first.

    The processors are built differently in how they address memory when they store the contents of an internal register.

    If you know the type of machine you have (you don't mention it in the post), then you can reconstruct the value by reassembling them in the order approprate to your endianness.

    This can be an issue when exchanging data over a network in byte streams. The network is big endian. Your library will provide functions called hton and ntoh (or similar). These translate the host to network order and vice versa. For a big endian machine, these will be empty functions, because the order is already correct. For an Intel machine, these functions will correctly reorder the bytes.
     

Share This Page