StackStalk
  • Home
  • Java
    • Java Collection
    • Spring Boot Collection
  • Python
    • Python Collection
  • C++
    • C++ Collection
    • Progamming Problems
    • Algorithms
    • Data Structures
    • Design Patterns
  • General
    • Tips and Tricks

Thursday, June 26, 2008

C++ Bit Fields

 June 26, 2008     CPP     No comments   

What are bit-fields?

  • Bit fields provide a mechanism to optimize memory usage by allowing to specify the exact number of bits required to store data.
  • Quite useful in embedded programming like mobile phones where memory is limited.
  • The declaration of bit field members follow the syntax "variable name : number of bits".
  • Unnamed bit fields with width 0 are used for alignment of the next bit field to the field type boundary.

Demonstrate the usage of bit fields

#include <iostream>
#include <assert>
using namespace std;

class MyTime {

unsigned hour : 5;
unsigned mins : 6;
unsigned secs : 6;

public:

void SetHour(int aHour) {
assert ( aHour < 24 );
hour = aHour;
}

void SetMins(int aMins) {
assert ( aMins < 60 );
mins = aMins;
}

void SetSecs(int aSecs) {
assert ( aSecs < 60 );
secs = aSecs;
}

void Print() {
cout << hour << ":" << mins << ":" << secs << endl;
}
};

int main()
{
MyTime t;
t.SetHour(12);
t.SetMins(58);
t.SetSecs(23);
t.Print();

cout << "Size of MyTime = " << sizeof(t) << endl;
}
OUTPUT:-
12:58:23
Size of MyTime = 4
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Follow @StackStalk
Get new posts by email:
Powered by follow.it

Popular Posts

  • Python FastAPI file upload and download
    In this article, we will look at an example of how to implement a file upload and download API in a Python FastAPI microservice. Example bel...
  • Avro Producer and Consumer with Python using Confluent Kafka
    In this article, we will understand Avro a popular data serialization format in streaming data applications and develop a simple Avro Produc...
  • Monitor Spring Boot App with Micrometer and Prometheus
    Modern distributed applications typically have multiple microservices working together. Ability to monitor and manage aspects like health, m...
  • Server-Sent Events with Spring WebFlux
    In this article we will review the concepts of server-sent events and work on an example using WebFlux. Before getting into this article it ...
  • Accessing the Kubernetes API
    In this article, we will explore the steps required to access the Kubernetes API and overcome common challenges. All operations and communic...
  • Python FastAPI microservice with Okta and OPA
    Authentication (AuthN) and Authorization (AuthZ) is a common challenge when developing microservices. In this article, we will explore how t...
  • Scheduling jobs in Python
    When developing applications and microservices we run into scenarios where there is a need to run scheduled tasks. Examples include performi...
  • Using Tekton to deploy KNative services
    Tekton is a popular open-source framework for building continuous delivery pipelines. Tekton provides a declarative way to define pipelines ...

Copyright © StackStalk