473,385 Members | 1,396 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,385 software developers and data experts.

bitwise copying here?

In the code below, will the assignment z= y do bitwise copying?

struct x
{
int a;
int b;
int c;
};

main()
{
struct x z, y;
...
z = y;

}

Sep 28 '06 #1
10 4196
webfan wrote:
In the code below, will the assignment z= y do bitwise copying?

struct x
{
int a;
int b;
int c;
};

main()
{
struct x z, y;
...
z = y;

}
Probably, but not guaranteedly.
What is guaranteed is that you have the effect of
z.a = y.a;
z.b = y.b;
z.c = y.c;

If there are two possible representations for one of these values,
then the assignment may change the representation.

If, for some strange reason, struct x has padding bytes, then
the representation of a, b, and c may be the same while the
representation of the padding bytes is different.

If you want a bit-by-bit copy, use
memcpy(&z, &y, sizeof z);
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Sep 28 '06 #2

Michael Mair wrote:
If you want a bit-by-bit copy, use
memcpy(&z, &y, sizeof z);
memcpy does a byte-by-byte copy not bit-by-bit

-kondal

Sep 29 '06 #3
kondal wrote:
Michael Mair wrote:

>>If you want a bit-by-bit copy, use
memcpy(&z, &y, sizeof z);

memcpy does a byte-by-byte copy not bit-by-bit
memcpy() may do a byte-by-byte copy, or bit-by-bit, or
nybble-by-nybble, or something else. The Standard says only
that it "copies n characters," but says nothing about how
those characters are to be copied. As a practical matter,
this allows a memcpy() implementation to copy four-byte words
or eight-byte octawords or other larger-than-byte-sized units,
if it can profitably do so.

From the point of view of a strictly conforming program,
the difference (if there is one) is subtle to the point of
being undetectably invisible.

--
Eric Sosman
es*****@acm-dot-org.invalid
Sep 29 '06 #4
kondal wrote:
memcpy does a byte-by-byte copy not bit-by-bit
There's no guarantee that it's done byte-by-byte.
If your underlying architecture has a block copy,
a memcpy implementation would be allowed to use it.

The motivation for mem* library routines is to allow
functions that have greater efficiency than straightforward
array copying.

There are common architectures that provide fast block-copy
instructions, and it would be a shame if a C library didn't
take advantage of them.
Sep 29 '06 #5
Michael Mair wrote:
webfan wrote:
>In the code below, will the assignment z= y do bitwise copying?

struct x
{
int a;
int b;
int c;
};

main()
{
struct x z, y;
...
z = y;

}


Probably, but not guaranteedly.
What is guaranteed is that you have the effect of
z.a = y.a;
z.b = y.b;
z.c = y.c;

If there are two possible representations for one of these values,
then the assignment may change the representation.

If, for some strange reason, struct x has padding bytes, then
the representation of a, b, and c may be the same while the
representation of the padding bytes is different.
I don't understand it well. Can you take an example?
Thank you
>
If you want a bit-by-bit copy, use
memcpy(&z, &y, sizeof z);
Cheers
Michael
Sep 29 '06 #6
kondal wrote:
Michael Mair wrote:
>>If you want a bit-by-bit copy, use
memcpy(&z, &y, sizeof z);

memcpy does a byte-by-byte copy not bit-by-bit
So what? Note that I took up the OP's wording.
unsigned char has no padding bits, so the representation of
y is copied to z. Of course, we only stop the copying at byte
borders -- but the result does not differ from bitwise copying.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Sep 29 '06 #7
zhengda wrote:
Michael Mair wrote:
>webfan wrote:
>>In the code below, will the assignment z= y do bitwise copying?

struct x
{
int a;
int b;
int c;
};

main()
{
struct x z, y;
...
z = y;

}

Probably, but not guaranteedly.
What is guaranteed is that you have the effect of
z.a = y.a;
z.b = y.b;
z.c = y.c;

If there are two possible representations for one of these values,
then the assignment may change the representation.

If, for some strange reason, struct x has padding bytes, then
the representation of a, b, and c may be the same while the
representation of the padding bytes is different.

I don't understand it well. Can you take an example?
Example padding bytes:
Say, sizeof int == 2, CHAR_BIT == 8 (i.e., we have 16 bit ints)
and access to 8 byte storage regions is somehow "more efficient"
than three times 2 bytes. Then, struct x might have the following
layout:
first two bytes -- a
second two bytes -- b
third two bytes -- c
last two bytes -- padding
i.e. sizeof (struct x) == 8 instead of the minimal possible 6.
Writing z = y does not say what happens to the padding -- it might
be copied over or not.

Example different representations[*]:
Say, we have one's complement. This gives us an all bits zero
representation of 0 and and all bits one representation for 0,
a "negative zero". If the implementation wants to eliminate
occurrences of negative zero, it could do so at copying, i.e.
y.a is all bits one and z.a becomes all bits zero but z.a == y.a.
Cheers
Michael
[*] It is possible that I misunderstood what the implementation
may and may not do with negative zeros. In that case, we can consider
padding bits that are not copied over.
I thought the negative zero example more interesting.
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Sep 29 '06 #8
Michael Mair wrote:
zhengda wrote:
>Michael Mair wrote:
>>webfan wrote:

In the code below, will the assignment z= y do bitwise copying?

struct x
{
int a;
int b;
int c;
};

main()
{
struct x z, y;
...
z = y;

}


Probably, but not guaranteedly.
What is guaranteed is that you have the effect of
z.a = y.a;
z.b = y.b;
z.c = y.c;

If there are two possible representations for one of these values,
then the assignment may change the representation.

If, for some strange reason, struct x has padding bytes, then
the representation of a, b, and c may be the same while the
representation of the padding bytes is different.


I don't understand it well. Can you take an example?


Example padding bytes:
Say, sizeof int == 2, CHAR_BIT == 8 (i.e., we have 16 bit ints)
and access to 8 byte storage regions is somehow "more efficient"
than three times 2 bytes. Then, struct x might have the following
layout:
first two bytes -- a
second two bytes -- b
third two bytes -- c
last two bytes -- padding
i.e. sizeof (struct x) == 8 instead of the minimal possible 6.
Writing z = y does not say what happens to the padding -- it might
be copied over or not.

Example different representations[*]:
Say, we have one's complement. This gives us an all bits zero
representation of 0 and and all bits one representation for 0,
a "negative zero". If the implementation wants to eliminate
I think the complement of -1 is the one whose bits are all 1.
occurrences of negative zero, it could do so at copying, i.e.
y.a is all bits one and z.a becomes all bits zero but z.a == y.a.
Cheers
Michael
[*] It is possible that I misunderstood what the implementation
may and may not do with negative zeros. In that case, we can consider
padding bits that are not copied over.
I thought the negative zero example more interesting.
Sep 29 '06 #9
zhengda wrote:
Michael Mair wrote:
>zhengda wrote:
<snip>
>>>If there are two possible representations for one of these values,
then the assignment may change the representation.
<snip>
>>>
I don't understand it well. Can you take an example?
<snip>
>>
Example different representations[*]:
Say, we have one's complement. This gives us an all bits zero
representation of 0 and and all bits one representation for 0,
a "negative zero". If the implementation wants to eliminate

I think the complement of -1 is the one whose bits are all 1.
One's complement is a system for binary representation of numbers.
It is different from two's complement (which you are talking about).
In one's complement, -number == ~number.

Have a look at
http://en.wikipedia.org/wiki/Signed_...epresentations
> occurrences of negative zero, it could do so at copying, i.e.
y.a is all bits one and z.a becomes all bits zero but z.a == y.a.
<snip>
>>
[*] It is possible that I misunderstood what the implementation
may and may not do with negative zeros. In that case, we can consider
padding bits that are not copied over.
I thought the negative zero example more interesting.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Sep 29 '06 #10
webfan posted:
In the code below, will the assignment z= y do bitwise copying?

struct x
{
int a;
int b;
int c;
};

main()
{
struct x z, y;
...
z = y;

}

No, memberwise copy. The difference is that any padding between members is
not guaranteed to be replicated (maybe it's even guaranteed _not_ to be
replicated, but I'm not sure).

As others have stated, memcpy is useful for bitwise copy. Or perhaps the
following if you're fond of DIY:

struct MyStruct {
int a,b,c;
};

int main(void)
{
struct MyStruct y = {5,4,2},z;

char unsigned const *p = (char unsigned const*)&y;
char unsigned const *const pover = p+sizeof y;

char unsigned *q = (char unsigned*)&z;

do *q++ = *p++; while(p!=pover);

return 0;
}

--

Frederick Gotham
Sep 29 '06 #11

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

Similar topics

9
by: Michael B. Trausch | last post by:
I have a question regarding bitwise operators, I've been trying to figure this out for about two days now, and I just can't seem to get it. What I'm trying to do is use a variable to hold a bitmask...
6
by: jas_lx | last post by:
The basic understanding of what bitwise operators (& ^ | >> << ) comes fairly simple, as long as one has a fundamental understanding of bits, bytes and binary. Having done some Win32...
2
by: Steve Summit | last post by:
-----BEGIN PGP SIGNED MESSAGE----- It's often explained that the reason for some of the imprecision in C's definition is so that C can be implemented on different kinds of machines -- say, those...
5
by: James Dean | last post by:
I am recoding a project in C#.....i just wanted to know if these are equivalent and give the same result.... old C++ code for ( long loop = 0; loop < ( longWidth_bytes - 1); loop++) { *lpbLine...
10
by: Emilio | last post by:
Do I use 'or' for bitwise operations where in c# I use | ?
3
by: Jay Ruyle | last post by:
I'm trying to figure out a way to list several items in a listbox and let the user select any number of items in the listbox. I have tried to code in the items as bitwise items but all it stores...
5
by: Gigs_ | last post by:
Can someone explain me bitwise expression? few examples for every expression will be nice x << y Left shift x >y Right shift x & y Bitwise AND x | y Bitwise OR x ^ y Bitwise XOR (exclusive...
2
bajajv
by: bajajv | last post by:
Hi, I am confused regarding these concepts. Please clearify. 1. Why is bitwise copying unsafe? 2. Does copy constructor does bitwise copying? 3. Is the main use of explicit copy constructor is...
29
by: Carl Banks | last post by:
Anyone with me here? (I know the deadline for P3 PEPs has passed; this is just talk.) Not many people are bit-fiddling these days. One of the main uses of bit fields is flags, but that's not...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.