473,407 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

union and bits

Hello All,

typedef union
{
int m_4ByteData;
char m_8bitData[4];
}data;

data conv;
conv.m_4ByteData = 1000;

I am using above union i am getting all the individual 8 bits ( 8 * 4 =
32 bits for int ). How to get the int value ( m_4ByteData) using
m_8bitData[4].

Thanks in advance
Chandra-
Nov 13 '05 #1
3 4871
Just access the structure

typedef union
{
int m_4ByteData;
char m_8bitData[4];
}data;

data conv;
conv.m_4ByteData = 1000;
printf("m_8bitData = %d %d %d %d\n",
conv.m_8bitData[0],
conv.m_8bitData[1],
conv.m_8bitData[2],
conv.m_8bitData[3]);

It has the same memmory attached to it

so &(conv.m_4ByteData) == &(conv.m_8bitData[0]) is always TRUE.

Greetings Olaf

chandra sekhar nunna <n.*************@sisl.co.in> wrote in message news:<3F*************@sisl.co.in>...
Hello All,

typedef union
{
int m_4ByteData;
char m_8bitData[4];
}data;

data conv;
conv.m_4ByteData = 1000;

I am using above union i am getting all the individual 8 bits ( 8 * 4 =
32 bits for int ). How to get the int value ( m_4ByteData) using
m_8bitData[4].

Thanks in advance
Chandra-

Nov 13 '05 #2
Hi,

Any idea how round double values using the same concept.

Bye
Chandra-

olaf wrote:
Just access the structure

typedef union
{
int m_4ByteData;
char m_8bitData[4];
}data;

data conv;
conv.m_4ByteData = 1000;
printf("m_8bitData = %d %d %d %d\n",
conv.m_8bitData[0],
conv.m_8bitData[1],
conv.m_8bitData[2],
conv.m_8bitData[3]);

It has the same memmory attached to it

so &(conv.m_4ByteData) == &(conv.m_8bitData[0]) is always TRUE.

Greetings Olaf

chandra sekhar nunna <n.*************@sisl.co.in> wrote in message news:<3F*************@sisl.co.in>...
Hello All,

typedef union
{
int m_4ByteData;
char m_8bitData[4];
}data;

data conv;
conv.m_4ByteData = 1000;

I am using above union i am getting all the individual 8 bits ( 8 * 4 =
32 bits for int ). How to get the int value ( m_4ByteData) using
m_8bitData[4].

Thanks in advance
Chandra-


Nov 13 '05 #3
In <3F*************@sisl.co.in> chandra sekhar nunna <n.*************@sisl.co.in> writes:
Hello All,

typedef union
{
int m_4ByteData;
char m_8bitData[4];
}data;

data conv;
conv.m_4ByteData = 1000;

I am using above union i am getting all the individual 8 bits ( 8 * 4 =
32 bits for int ). How to get the int value ( m_4ByteData) using
m_8bitData[4].


Put the stuff into m_8bitData[4] and read the value of m_4ByteData.
In theory, this invokes undefined behaviour, in practice, it works.

You may want to replace the magical constant 4 by sizeof(int) and to
use the type unsigned char for m_8bitData[4].

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #4

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

Similar topics

2
by: Claudio | last post by:
hi all i put in this email source code so u can copy and paste to verify strange first : in this example bit size is a BYTE ?! second : in the last printf output is wrong ? ? best...
16
by: Bun Head | last post by:
Does anyone see a problem with.. #include <stdio.h> typedef struct { float x; float y; float z; } _XYZ_;
14
by: Clint Olsen | last post by:
I was wondering if it's considered undefined behavior to use a member of a union when it wasn't initialized with that member. Example: typedef unsigned long hval_t; hval_t hval_init(void) {...
16
by: tedu | last post by:
does anyone know of a platform/compiler which will place union elements to not overlap? as in union u { int a; long b; size_t c; }; in my limited experience, writing to any of (a, b, or c)...
7
by: felixnielsen | last post by:
I would wery much like this to work: @code start #include <iostream> #include <bitset> const unsigned short size = 2; // 2^<unsigned int> // union V { char c; std::bitset<size*size*size> b;...
50
by: Mikhail Teterin | last post by:
Hello! The sample program below is compiled fine by gcc (with -Wall), but rejected by Sun's SUNWspro compiler (version 6 update 2). The point of contention is, whether a value for one of the...
21
by: Hallvard B Furuseth | last post by:
Is the code below valid? Generally a value must be accessed through the same type it was stored as, but there is an exception for data stored through a character type. I'm not sure if that...
29
by: Virtual_X | last post by:
As in IEEE754 double consist of sign bit 11 bits for exponent 52 bits for fraction i write this code to print double parts as it explained in ieee754 i want to know if the code contain any...
2
by: chang | last post by:
Hi All, have some experience in C prog language. I have small doubt abt using unions in C language. Here is a small programm in vc++: union a{ int b; char c; }d;
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.