www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/04/15/13:16:40

Message-ID: <19980415171434.9744.qmail@hotmail.com>
From: "Trond Endrestol" <endrestol AT hotmail DOT com>
To: space AT gold DOT com DOT br
Cc: djgpp AT delorie DOT com
Subject: Re: Individual Bits
Date: Wed, 15 Apr 1998 19:14:33 CEST

Hi!

You must use bit fields, a struct, and a union (see my example below).

Hope this helps.

Trond Endrestøl.

------------------------------------------------------------------
#include <iostream.h>

typedef struct bits_in_a_byte {
  int Bit01 : 1;
  int Bit02 : 1;
  int Bit03 : 1;
  int Bit04 : 1;
  int Bit05 : 1;
  int Bit06 : 1;
  int Bit07 : 1;
  int Bit08 : 1;
} bits_in_a_byte;

typedef unsigned char byte;

typedef union byte_and_bits {
  byte Byte;
  bits_in_a_byte Bits;
} byte_and_bits;

int main()
{
  byte_and_bits MyByte;

  MyByte.Byte = 55;

  cout << (unsigned int)MyByte.Byte << endl; // Should display 55

  MyByte.Bits.Bit08 = 1;

  cout << (unsigned int)MyByte.Byte << endl; // Should display 183

  return 0;
}


______________________________________________________
Get Your Private, Free Email at http://www.hotmail.com

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019