Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6045189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:05:50+00:00 2026-05-23T07:05:50+00:00

#include <stdio.h> typedef struct _octet { char tft_op:3; char e_bit:1; char no_of_pkt:4; }octet_t; int

  • 0
#include <stdio.h>
typedef struct _octet
{
char tft_op:3;
char e_bit:1;
char no_of_pkt:4;
}octet_t;

int main()
{
octet_t st_octet;
st_octet.tft_op = 0x8;
st_octet.e_bit = 0x1;
st_octet.no_of_pkt = 0x10;
char *ptr = (char*)&st_octet;
printf("%#x\n",*ptr);
return 0;
}

here i want to store 3 bits in tft_op, 1 bit e_bit, 4 bits in no_of_pkt, but i want the o/p as a combined 1 byte i.e is want the o/p as FA but the o/p i get is 0x8 what should i do to get the o/p as FA

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:05:50+00:00Added an answer on May 23, 2026 at 7:05 am

    Your values are too big for the allocated space:

    0x8 = 1000b - four bits, not three
    0x1 = 1b - but if char is signed you can only have 0 and -1!
    0x10 = 16 = 10000b - five bits, you'be allocated four
    

    So, the effective result is:

    (0x8 | (0x1 << 3) | (0x10 << 4)) & 0xff
    
    => (0x8 | 0x8 | 0x100) & 0xff
    
    => 0x8
    

    which is what you’re getting. 0xfa is:

    11111010b
    

    but you assume that the first item in the structure is at bit position 7 (the most significant) but the standard (as far as I know) doesn’t specify where the bits should start (it could be bit0, the least significant) and looking at your output I guess the first field is the least significant bit, so you have:

    ccccbaaa
    

    where a is tft_op, b is e_bit and c is no_of_pkt. To get 0xfa it’s either:

    tft_op = 2
    e_bit = 1
    no_of_pkt = 15
    

    or, if the order is the other way round:

    tft_op = 7
    e_bit = 1
    no_of_pkt = 10
    

    Also, rather than doing char *ptr = (char*)&st_octet; look into using a union:

    typedef union
    {
      struct _octet
      {
        char tft_op:3;
        char e_bit:1;
        char no_of_pkt:4;
      };
      char data;
    } octet_t;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <stdio.h> #include <stdlib.h> typedef struct { char *Name; int grade; int cost; }Hotel;
#include <stdio.h> typedef struct point{ int x; int y; }; void main (void){ struct
#include <stdio.h> typedef struct pduct {char name[20]; int price; int stock;} PRODUCT; void init(PRODUCT
#include <stdio.h> #include <stdlib.h> typedef int element; struct cell { element e; struct cell
When I run this code: #include <stdio.h> typedef struct _Food { char name [128];
In the following piece of code, #include<stdio.h> typedef struct { int bit1:1; int bit3:4;
I have the following C-code: #include<stdio.h> #include<stdlib.h> typedef struct node { int a; }node;
#include <stdio.h> typedef struct node { int i; struct node *next; }node; node getnode(int
#include <stdio.h> typedef struct size { unsigned int a:1; unsigned int b:31; unsigned int
I have following code #include <stdio.h> #include<ctype.h> typedef struct { int Type; int Type2;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.