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 7078697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:34:46+00:00 2026-05-28T06:34:46+00:00

Could someone please take a look at this sloppy code and explain to me

  • 0

Could someone please take a look at this sloppy code and explain to me why it does not work. Am I packing and unpacking things correctly? (the object of this lab was to pack a date using bit shifting and masking. For example console input of 31/12/99 would be OR’ed together then AND’ed out, which is what my code was attempting to do. Thanks.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#define DAY_MASK 0x3e0
#define MONTH_MASK 0xc00
#define YEAR_MASK (~0x3180)

void hr()
{
    printf("-----------------------------------------------\n");
}

void fields()
{
    printf("     Binary\t\tDTG\t\tBase 10\n");
}

void prnFields(unsigned int *day, unsigned int *month, unsigned int *year)
{
    printBits(day);
    printf("\tDay\t\t%u\n", day);
    printBits(month);
    printf("\tMonth\t\t%u\n", month);
    printBits(year);
    printf("\tYear\t\t%u\n", year);
}

int main()
{
    unsigned int day;
    unsigned int month;
    unsigned int year;
    unsigned int packed;

    printf("Enter numeric Day\t:");
    scanf("%d", &day);
    printf("Enter numeric Month\t:");
    scanf("%d", &month);
    printf("Enter two digit Year\t:");
    scanf("%d", &year);
    printf("\n");

    hr();
    printf("\nPrepacked Date\n");
    fields();
    hr();
    prnFields(day, month, year);
    hr();

    packed = day; packed <<= 9;
    packed |= month; packed <<= 4;
    packed |= year;
    printf("\nPacked Date\n");
    fields();
    hr();
    printBits(packed);printf("\t\t\t%d\n", packed);
    hr();
    printf("\nUnpacked Date\n");
    fields();
    hr();
    printBits((packed & DAY_MASK));
    printf("\tDay\t\t%d \n", (packed & DAY_MASK) >> 9);
    printBits((packed & MONTH_MASK));
    printf("\tMonth\t\t%d \n", (packed & MONTH_MASK) >> 5);
    printBits((packed & YEAR_MASK));
    printf("\tYear\t\t%d \n", (packed & YEAR_MASK));
     //system("pause");
    return(0);
}

void printBits(unsigned short int value)
{
    unsigned short int mask =1;
    int i;
    mask<<=15;

    for(i=1; i<=16; i++)
    {
        putchar( (mask&value)? '1': '0');

        if(i%8==0)
        {
            putchar(' ');
        }

        value<<=1;
    }
}
  • 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-28T06:34:47+00:00Added an answer on May 28, 2026 at 6:34 am

    You seem to have too few bits assigned for Month (0xc00) and the way you do it it’s not easy to see whether your shifts are correct.

    I’d suggest to define your constants in a more consistent way like this:

    #define DAY_BITS   5
    #define MONTH_BITS 4
    #define YEAR_BITS  7
    
    #define DAY_OFFSET   YEAR_BITS
    #define MONTH_OFFSET ( YEAR_BITS + DAY_BITS )
    #define YEAR_OFFSET  0
    
    #define DAY_MASK   ~( ~0 << DAY_BITS   )
    #define MONTH_MASK ~( ~0 << MONTH_BITS )
    #define YEAR_MASK  ~( ~0 << YEAR_BITS  )
    

    … now you can set the packed value like this:

    packed = 0;
    packed |= ( day   & DAY_MASK   ) << DAY_OFFSET;
    packed |= ( month & MONTH_MASK ) << MONTH_OFFSET;
    packed |= ( year  & YEAR_MASK  ) << YEAR_OFFSET;
    

    … and get the single fields like this:

    printf("\tDay\t\t%d \n",   ( packed >> DAY_OFFSET   ) & DAY_MASK );
    printf("\tMonth\t\t%d \n", ( packed >> MONTH_OFFSET ) & MONTH_MASK );
    printf("\tYear\t\t%d \n",  ( packed >> YEAR_OFFSET  ) & YEAR_MASK );
    

    You could now simply change the order of the fields in your offset definitions to make the dates easily sortable:

    #define DAY_OFFSET   0
    #define MONTH_OFFSET DAY_BITS
    #define YEAR_OFFSET  ( DAY_BITS + MONTH_BITS )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could someone take a look at this code and find out what's wrong with
Could someone please take a look at this ? You'll see the first alert
Would someone please take a look at the following code fragments (all from one
Hi could some one please take a look at this and let me know
Could someone please take a few moments out of their schedule to look at
Could someone please share the code to take screenshots of web browser control and
Could someone please explain the best way to connect to an Interbase 7.1 database
Could someone please explain? I couldn't find anything on the internet, everything talks about
Could someone please explain when would I want to use delegation instead of inheritance?
These few lines of code are giving me a java.lang.ArrayIndexOutOfBoundsException exception, could someone please

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.