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

  • SEARCH
  • Home
  • 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 6788837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:31:57+00:00 2026-05-26T17:31:57+00:00

I have a unsigned 64-bit word and a bit padded structure which are both

  • 0

I have a unsigned 64-bit word and a bit padded structure which are both given below.The structure is inside a union which contains several(11 to be exact) similar but slightly structures.

uint64_t final_data_word;

#pragma pack(1)
typedef struct control_block_format_1_s
{
    uint8_t block_type_field:8;
    uint8_t control_word_0:7;
    uint8_t control_word_1:7;
    uint8_t control_word_2:7;
    uint8_t control_word_3:7;
    uint8_t control_word_4:7;
    uint8_t control_word_5:7;
    uint8_t control_word_6:7;
    uint8_t control_word_7:7;
}control_block_format_1_t;

typedef union
{
    control_block_format_1_t *cb_1;
    control_block_format_2_t *cb_2;
    control_block_format_3_t *cb_3;
    control_block_format_4_t *cb_4;
    control_block_format_5_t *cb_5;
    control_block_format_6_t *cb_6;
    control_block_format_7_t *cb_7;
    control_block_format_8_t *cb_8;
    control_block_format_9_t *cb_9;
    control_block_format_10_t *cb_10;
    control_block_format_11_t *cb_11;
}block_payload_union_t;
#pragma pack()

I want to interpret the 64 bits in the 64-bit word as fields in the structure.so I am doing the below operation

block_payload_union_t *block_pload =(block_payload_union_t*)malloc(sizeof(block_payload_union_t*));
block_pload->cb_1 = (control_block_format_1_t*)(&final_data_word);

but I am not getting the expected values for the last field in my structure.Can anyone see any problem with what I am doing?Any suggestions or comments are appreciated.

@Jonathan
I have added the following comments to my code.

printf(“sizeof(union) = %zu\n”, sizeof(block_payload_union_t));

printf(“sizeof(cb1) = %zu\n”, sizeof(control_block_format_1_t));

printf(“FDW = 0x%.16lx\n”, final_data_word);

//printf(“*bp->llp = 0x%.16lx\n”, *block_pload->llp);

printf(“bp->cb1->block_type_fld = 0x%.2X\n”, block_pload->cb_1->block_type_field);

printf(“bp->cb1->control_word_0 = 0x%.2X\n”, block_pload->cb_1->control_word_0);

printf(“bp->cb1->control_word_1 = 0x%.2X\n”, block_pload->cb_1->control_word_1);

printf(“bp->cb1->control_word_2 = 0x%.2X\n”, block_pload->cb_1->control_word_2);

printf(“bp->cb1->control_word_3 = 0x%.2X\n”, block_pload->cb_1->control_word_3);

printf(“bp->cb1->control_word_4 = 0x%.2X\n”, block_pload->cb_1->control_word_4);

printf(“bp->cb1->control_word_5 = 0x%.2X\n”, block_pload->cb_1->control_word_5);

printf(“bp->cb1->control_word_6 = 0x%.2X\n”, block_pload->cb_1->control_word_6);

printf(“bp->cb1->control_word_7 = 0x%.2X\n”, block_pload->cb_1->control_word_7);

The output I got without #pragma pack() was as follows

final data word 0x1e00000000000000

sizeof(union) = 8

sizeof(cb1) = 9

FDW = 0x1e00000000000000

bp->cb1->block_type_fld = 0x00

bp->cb1->control_word_0 = 0x00

bp->cb1->control_word_1 = 0x00

bp->cb1->control_word_2 = 0x00

bp->cb1->control_word_3 = 0x00

bp->cb1->control_word_4 = 0x00

bp->cb1->control_word_5 = 0x00

bp->cb1->control_word_6 = 0x1E

bp->cb1->control_word_7 = 0x78

The output with #pragma pack() was as follows

final data word 0x1e00000000000000

sizeof(union) = 8

sizeof(cb1) = 8

FDW = 0x1e00000000000000

bp->cb1->block_type_fld = 0x00

bp->cb1->control_word_0 = 0x00

bp->cb1->control_word_1 = 0x00

bp->cb1->control_word_2 = 0x00

bp->cb1->control_word_3 = 0x00

bp->cb1->control_word_4 = 0x00

bp->cb1->control_word_5 = 0x00

bp->cb1->control_word_6 = 0x00

bp->cb1->control_word_7 = 0x0F

which is similar to the output you got on Jonathan’s machine.

  • 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-26T17:31:57+00:00Added an answer on May 26, 2026 at 5:31 pm

    You should be using sizeof(block_payload_union_t) instead of sizeof(block_payload_union_t *) in the call to malloc(). However, on a 64-bit machine, it probably gives you the same size (8), so you get away with it, for all it is wrong.

    It is slightly odd that your block_payload_union_t contains pointers to your field layouts instead of holding the actual values.

    You have not shown us the declaration of final_data_word. Have you checked the size of your union versus the size you expect?


    On a Mac running Lion (10.7.2), I get this output from the program which follows:

    Output

    With #pragma pack(1):

    sizeof(union) = 8
    sizeof(cb1) = 8
    FDW = 0xFEDCBA9876543210
    *bp->llp = 0xFEDCBA9876543210
    bp->cb1->block_type_fld = 0x10
    bp->cb1->control_word_0 = 0x32
    bp->cb1->control_word_1 = 0x28
    bp->cb1->control_word_2 = 0x59
    bp->cb1->control_word_3 = 0x43
    bp->cb1->control_word_4 = 0x29
    bp->cb1->control_word_5 = 0x17
    bp->cb1->control_word_6 = 0x37
    bp->cb1->control_word_7 = 0x7F
    

    Without #pragma pack(1):

    sizeof(union) = 8
    sizeof(cb1) = 9
    FDW = 0xFEDCBA9876543210
    *bp->llp = 0xFEDCBA9876543210
    bp->cb1->block_type_fld = 0x10
    bp->cb1->control_word_0 = 0x32
    bp->cb1->control_word_1 = 0x54
    bp->cb1->control_word_2 = 0x76
    bp->cb1->control_word_3 = 0x18
    bp->cb1->control_word_4 = 0x3A
    bp->cb1->control_word_5 = 0x5C
    bp->cb1->control_word_6 = 0x7E
    bp->cb1->control_word_7 = 0x10
    

    What are you getting?

    Program

    #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    
    #pragma pack(1)
    typedef struct control_block_format_1_s
    {
        uint8_t block_type_field:8;
        uint8_t control_word_0:7;
        uint8_t control_word_1:7;
        uint8_t control_word_2:7;
        uint8_t control_word_3:7;
        uint8_t control_word_4:7;
        uint8_t control_word_5:7;
        uint8_t control_word_6:7;
        uint8_t control_word_7:7;
    } control_block_format_1_t;
    
    typedef union
    {
        long long *llp;
        control_block_format_1_t *cb_1;
        //control_block_format_2_t *cb_2;
        //control_block_format_3_t *cb_3;
        //control_block_format_4_t *cb_4;
        //control_block_format_5_t *cb_5;
        //control_block_format_6_t *cb_6;
        //control_block_format_7_t *cb_7;
        //control_block_format_8_t *cb_8;
        //control_block_format_9_t *cb_9;
        //control_block_format_10_t *cb_10;
        //control_block_format_11_t *cb_11;
    } block_payload_union_t;
    #pragma pack()
    
    int main(void)
    {
        long long final_data_word = 0xFEDCBA9876543210;
        block_payload_union_t *block_pload =(block_payload_union_t*)malloc(sizeof(block_payload_union_t));
        block_pload->cb_1 = (control_block_format_1_t*)(&final_data_word);
        printf("sizeof(union) = %zu\n", sizeof(block_payload_union_t));
        printf("sizeof(cb1) = %zu\n", sizeof(control_block_format_1_t));
        printf("FDW = 0x%.16llX\n", final_data_word);
        printf("*bp->llp = 0x%.16llX\n", *block_pload->llp);
        printf("bp->cb1->block_type_fld = 0x%.2X\n", block_pload->cb_1->block_type_field);
        printf("bp->cb1->control_word_0 = 0x%.2X\n", block_pload->cb_1->control_word_0);
        printf("bp->cb1->control_word_1 = 0x%.2X\n", block_pload->cb_1->control_word_1);
        printf("bp->cb1->control_word_2 = 0x%.2X\n", block_pload->cb_1->control_word_2);
        printf("bp->cb1->control_word_3 = 0x%.2X\n", block_pload->cb_1->control_word_3);
        printf("bp->cb1->control_word_4 = 0x%.2X\n", block_pload->cb_1->control_word_4);
        printf("bp->cb1->control_word_5 = 0x%.2X\n", block_pload->cb_1->control_word_5);
        printf("bp->cb1->control_word_6 = 0x%.2X\n", block_pload->cb_1->control_word_6);
        printf("bp->cb1->control_word_7 = 0x%.2X\n", block_pload->cb_1->control_word_7);
        return(0);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a count register, which is made up of two 32-bit unsigned integers,
I have a function which needs the quotient and remainder for an unsigned 64-bit
I have values that are 64-bit unsigned ints, and I need to store them
Say we have an 8-bit unsigned integer n ( UINT8_MAX=255 ); what is the
How can you multiply two unsigned 23-bit numbers if you only have memory locations
You have an ASCII string representing a 128-bit unsigned integer number n, i.e. 0
I have a problem with bit shifting and unsigned longs. Here's my test code:
Basically I have 2 unsigned 8-bit binary numbers and I need to use Boolean
I have an unsigned 16-bit integer in big endian byte order: 0x01f1 but they
More specifically: I have a sequence of 32 bit unsigned RGBA integers for pixels-

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.