473,670 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

template and bit-field

Hello,

I am trying to use template programing and bit-field and I came up
with (*). I was not able to make a really clean solution but instead
separate into 3 cases:
1. mask is declare before the value
2. value is declare before the mask
3. for zero bit field mask need to be unnamed

Is there another solution that would have been cleaner (less
specific cases) ?

Thanks
-Mathieu

(*)
/*
Bits Allocated = 16
Bits Stored = 12
High Bit = 11

|<------------------ pixel -----------------
>|
______________ ______________ ______________
______________
|XXXXXXXXXXXXXX | | |
|
|______________ |______________ |______________ |
______________|
15 12 11 8 7 4 3
0

---------------------------

Bits Allocated = 16
Bits Stored = 12
High Bit = 15

|<------------------ pixel ----------------->|
______________ ______________ ______________
______________
| | | |
XXXXXXXXXXXXXX|
|______________ |______________ |______________ |
______________|
15 12 11 8 7 4 3
0

---------------------------

Bits Allocated = 12
Bits Stored = 12
High Bit = 11

------ 2 ----->|<------------------ pixel 1 ---------------
>|
______________ ______________ ______________
______________
| | | |
|
|______________ |______________ |______________ |
______________|
15 12 11 8 7 4 3
0
-------------- 3 ------------>|<------------ 2
--------------
______________ ______________ ______________
______________
| | | |
|
|______________ |______________ |______________ |
______________|
15 12 11 8 7 4 3
0
|<------------------ pixel 4 --------------->|<----- 3
------
______________ ______________ ______________
______________
| | | |
|
|______________ |______________ |______________ |
______________|
15 12 11 8 7 4 3
0
*/

#include <iostream>

template <intstruct BitsAllocatedTo Type;
template<struct BitsAllocatedTo Type<16{ typedef unsigned short
Type; };
template<struct BitsAllocatedTo Type<12{ typedef unsigned short
Type; };
template<struct BitsAllocatedTo Type<8{ typedef unsigned char
Type; };

template <int BitsAllocated, int BitsStored, int typestruct
PixelDetail;
// mask last:
template<int BitsAllocated, int BitsStored>
struct PixelDetail<Bit sAllocated, BitsStored, 1>
{
typedef typename BitsAllocatedTo Type<BitsAlloca ted>::Type Type;
public:
Type v:BitsStored;
private:
Type mask:(BitsAlloc ated-BitsStored);
};
// mask first:
template<int BitsAllocated, int BitsStored>
struct PixelDetail<Bit sAllocated, BitsStored, -1>
{
typedef typename BitsAllocatedTo Type<BitsAlloca ted>::Type Type;
private:
Type mask:(BitsAlloc ated-BitsStored);
public:
Type v:BitsStored;
};
// unamed zero bit-field:
template<int BitsAllocated, int BitsStored>
struct PixelDetail<Bit sAllocated, BitsStored, 0>
{
// static_assert( BitsAllocated == BitsStored )
typedef typename BitsAllocatedTo Type<BitsAlloca ted>::Type Type;
private:
Type :(BitsAllocated-BitsStored);
public:
Type v:BitsStored;
};
// Bits Allocated
// Bits Stored
// High Bit
template <int BitsAllocated, int BitsStored, int HighBits>
struct Pixel : public PixelDetail<Bit sAllocated, BitsStored,
(BitsAllocated - BitsStored) ? ((BitsStored < HighBits) ? 1 : -1) : 0>
{
typedef typename BitsAllocatedTo Type<BitsAlloca ted>::Type Type;
operator Type () const { return this->v; }
};

int main()
{
Pixel<16,12,15p ixel1;
Pixel<16,12,11p ixel2;
Pixel<12,12,11p ixel3;
Pixel<16,16,15p ixel4;

const unsigned short v = 0xcdef;
memcpy(&pixel1, &v,sizeof(unsig ned short));
memcpy(&pixel2, &v,sizeof(unsig ned short));
memcpy(&pixel3, &v,sizeof(unsig ned short));
memcpy(&pixel4, &v,sizeof(unsig ned short));

std::cout << std::hex << pixel1 << std::endl;
std::cout << std::hex << pixel2 << std::endl;
std::cout << std::hex << pixel3 << std::endl;
std::cout << std::hex << pixel4 << std::endl;

return 0;
}

Feb 17 '07 #1
0 1564

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2154
by: Nobody | last post by:
This is sort of my first attempt at writing a template container class, just wanted some feedback if everything looks kosher or if there can be any improvements. This is a template class for a binary search tree. Note there is a requirement for this to be a Win32/MFC "friendly" class, thus the use of CObject and POSITION. There is also a requirement for there not to be a separate iterator class. template <class TYPE, class ARG_TYPE =...
5
3030
by: Vijai Kalyan | last post by:
Hello, I have come back to C++ after a couple of years with Java so I am quite rusty and this question may seem poor: My platform is Windows XP with MSVC 7.1. I have a class with a templatized conversion operator defined as follows:
2
1777
by: Yang Zhang | last post by:
I have a small program like this: //////////////////////////////////////////////// #include <iostream> using namespace std ; // set bits in an address template <typename T> inline T* set_addr(const T* addr, unsigned long n) {
4
1612
by: softwareengineer2006 | last post by:
C++ Template - Does it work on 16-bit platform Hi All, I was using an open source wbxmlparser for my mobile application. Now I am supposed to port the mobile application into a 16-bit platform,
5
3600
by: mast2as | last post by:
Hi guys Here's the class I try to compile (see below). By itself when I have a test.cc file for example that creates an object which is an instance of the class SpectralProfile, it compiles fine. 1 / Now If I move the getSpectrumAtDistance( const T &dist ) method definition in SpectralProfofile.cc let's say the compiler says core/profile.cc:199: error: `kNumBins' was not declared in this scope
45
2886
by: charles.lobo | last post by:
Hi, I have recently begun using templates in C++ and have found it to be quite useful. However, hearing stories of code bloat and assorted problems I decided to write a couple of small programs to check. What I expected was that there would be minor code bloat and some speed improvement when using templates. However... I wrote a basic list container (using templates), and a list container (using virtual derived classes). I also tried...
4
3351
by: stinos | last post by:
Hi All! suppose a class having a function for outputting data somehow, class X { template< class tType > void Output( const tType& arg ) { //default ToString handles integers/doubles
2
2206
by: Gary Nastrasio | last post by:
I'm currently reading Andrei Alexandrescu's book "Modern C++ Design" and I'm a bit confused by one bit of template syntax in chapter 1. Here is a code example: template <class CreationPolicy> class WidgetManager : public Creation Policy {...} // Create an instance of WidgetManager which managers type Widget WidgetManager< OpNewCreator<Widget MyWidgetMgr;
3
2854
by: Pierre Yves | last post by:
Hi there, Template, once again... Nice topic but a bit tricky to code. My problem is the following: I have to video parser which retrieve image frames. The pixel values can be unsigned char (8 bit) or unsigned short (16 bit). I've got a templated frame:
1
2658
by: Alex Vinokur | last post by:
Hi, I have compilation problem on SUN CC compiler with template while using option -m64 (64-bit addressing model). No problem while using option -m32 (32-bit addressing model) $ CC -V CC: Sun C++ 5.9 SunOS_sparc Patch 124863-01 2007/07/25
0
8469
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8592
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8661
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7419
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4211
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4391
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2800
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1794
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.