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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:20:36+00:00 2026-06-02T16:20:36+00:00

Doing some experiences to understand bit fields Hi have this code: 01 #include <stdio.h>

  • 0

Doing some experiences to understand bit fields

Hi have this code:

01  #include <stdio.h>
02  #include <stdlib.h>
03
04  void AddToBitfield(int *bitfield, int bitCount, int value);
05  int ReadFromBitfield(int *bitfield, int bitCount);
06
07  int main(){
08      /*Device list (0 Modem,1 Keyboard,2 Mouse,3 Speakers,4 Joystick,5 Flash Drive,6 Scanner,7 Printer,8 Microphone,9 Webcam,10 Monitor)*/
09      int device=0,memLoc=0,data=0;
10      int number = 0;
11
12      memLoc = 01; /*put 01 or 10*/
13      device = 15; /*Device id*/
14      data = 12343; /*Data to store - Only less or equal then 65535*/
15
16      AddToBitfield(&number,4,device);
17      AddToBitfield(&number,16,data);
18      AddToBitfield(&number,2,memLoc);
19
20      printf("--%d---\n",number);
21
22      printf("Memory location: %d\n",ReadFromBitfield(&number,2));
23      printf("Data stored: %d\n",ReadFromBitfield(&number,16));
24      printf("Device: %d\n",ReadFromBitfield(&number,4));
25
26      return 0;
27  }
28
29  void AddToBitfield(int *bitfield, int bitCount, int value){
30      *bitfield <<= bitCount;
31      *bitfield |= value;
32  }
33
34  int ReadFromBitfield(int *bitfield, int bitCount){
35      int value = *bitfield & ((1 << bitCount) - 1);
36      *bitfield >>= bitCount;
37
38      return value;
39  }

Using data = 12343; and with memLoc = 01; or memLoc = 10; the printf will show all as expected.

Using data = 12346; and with memLoc = 01; or memLoc = 10; its the same thing. The printf will show all as expected.

But if I use data = 12344; or data = 12345; if I use memLoc = 01; the print will show what is expected in both cases, but if i use memLoc = 10; on the first case it will print Data stored: 12346 and on the second case Data stored: 12347.

Why is this happening?

  • 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-06-02T16:20:40+00:00Added an answer on June 2, 2026 at 4:20 pm

    You can use this C struct for reading/writing bit fields:

    struct MyType
    {
        uint16_t memLoc:4;
        uint16_t device:4;
        uint16_t _reserved:8;
        uint16_t data;
    };
    

    Your code will looks like this:

    int main()
    {
        // ...
    
        struct MyType number;
        uint32_t numberInt;
    
        number.memLoc = 1;
        number.device = 15;
        number.data = 12343;
    
        memcpy(&numberInt, &number, sizeof(struct MyType));
        printf("--%d---\n", numberInt);
    
        printf("Memory location: %d\n", number.memLoc);
        printf("Data stored: %d\n", number.data);
        printf("Device: %d\n", number.device);
    
        // ...
    }
    

    Tell me how many bits are in each field, and i’ll fix that structure for you.

    Here is an article about bit fields: http://en.wikipedia.org/wiki/Bit_field

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm doing some experiences with OOP in JavaScript. My goal is to have a
I'm doing some homework and while I have some experience with SML, Haskell has
I have Java experience and recently am doing some C++ coding. My question is
I have very little experience with ASP.NET and I am doing some self-training before
Doing some ETL and I have a wide file with lots of cols, for
Doing some jquery animation. I have certain divs set up with an attribute of
Have seen some conversations revolving around this, but hoping for some current input as
I am doing some maintenance on software and have a problem that I do
Doing some homework here (second assignment, still extremely green...). The object is to read
While doing some JavaScript performance tests I came up with the following piece of

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.