/*
It seems that when an int with width of four bytes is assigned to a one
byte width char, the first three bytes from left to right are discarded
and the rightest byte is assigned to that char.
So, I can just assign an int to a char to retrieve the rightest byte of
that int, and I also use this method plus right-shift operation to get
the leftest byte of an int.
I should be wrong on it, please correct me with your expertise.
Thank you
lovecreatesbeau ty
*/
#include <stdio.h>
int main(void)
{
unsigned int data = 'chum';
unsigned char left = '\0';
unsigned char right = '\0';
printf("data: %#X\n", data);
/* get the leftest byte of an int */
left = data >> (sizeof(int) - 1) * 8;
/* get the rightest byte of an int */
right = data;
printf("left: %#X, %c\n", left, left);
printf("right: %#X, %c\n", right, right);
return 0;
}
/* result: (Win2k, mingw32)
data: 0X6368756D
left: 0X63, c
right: 0X6D, m
*/ 6 2190
lovecreatesbeau ty wrote: /*
It seems that when an int with width of four bytes is assigned to a one byte width char, the first three bytes from left to right are discarded and the rightest byte is assigned to that char.
That's how it usually works out. From the language's
point view you're assigning values rather than chunks of
the representation, and the value stored in the char is
derived from the value of the int via a conversion.
Your example code uses unsigned ints and chars, for
which things will work as you describe. For signed variants
the question of representation comes into play, and things
could turn out differently on a system that doesn't use
two's complement arithmetic. Such systems are surpassingly
rare nowadays, but ...
So, I can just assign an int to a char to retrieve the rightest byte of that int, and I also use this method plus right-shift operation to get the leftest byte of an int.
Yes, for unsigned types. Be warned that negative integers
do not right-shift the same way on all systems.
I should be wrong on it, please correct me with your expertise.
Thank you
lovecreatesbeau ty
*/
#include <stdio.h>
int main(void) { unsigned int data = 'chum';
This construct isn't portable, and the value assigned
to data is entirely compiler-defined. Either the 'c' or
the 'm' -- or even neither! -- might wind up in the low-
order byte of data. A portable (assuming four-byte int)
way of ensuring the order you expect is to assemble it
from the individual pieces:
unsigned int data = ('c' << 24) + ('h' << 16)
+ ('u' << 8) + 'm';
unsigned char left = '\0'; unsigned char right = '\0';
printf("data: %#X\n", data);
/* get the leftest byte of an int */ left = data >> (sizeof(int) - 1) * 8;
This is fine, assuming eight-bit char. Machines
with other char sizes are rare, but definitely do exist
(they are commoner than machines that don't use two's
complement). To gain more portability, you could #include
the <limits.h> header and use CHAR_BIT instead of 8 (and
you'd make similar adjustments in the initialization of
data, above).
/* get the rightest byte of an int */ right = data;
printf("left: %#X, %c\n", left, left); printf("right: %#X, %c\n", right, right);
return 0; }
/* result: (Win2k, mingw32) data: 0X6368756D left: 0X63, c right: 0X6D, m */
A little thought will show that shifting by other
amounts can retrieve the 'h' and 'u' characters, too.
In fact, you can retrieve groups of bits that straddle
the char boundaries, if you want. See also "masking."
--
Eric Sosman es*****@acm-dot-org.invalid
In article <11************ **********@g49g 2000cwa.googleg roups.com>,
lovecreatesbeau ty <lo************ ***@gmail.com> wrote: It seems that when an int with width of four bytes is assigned to a one byte width char, the first three bytes from left to right are discarded and the rightest byte is assigned to that char.
What are "left" and "right" with respect to int?
The byte order of int in a variable is unspecified, and the
byte order of int in a CPU register might be different.
Your example is for "Win32", which (if I understand correctly) is
an OS implemented only for instruction sets that happen not to use
"bigendian" storage order in memory, and whose CPU registers are not
necessarily the same as their memory storage order.
If you define "left" as Most Significant Bit and "right" as
Least Signficant Bit, and the byte you want to get out is
the one containing the Least Significant Bits, and you recognize
that you are talking about *values* rather than about what might
happen to be stored in memory, and if you are use 'unsigned'
for the int and the char, and your int is 4 bytes, then Yes, a
simple assignment is certain to have that result.
So, I can just assign an int to a char to retrieve the rightest byte of that int, and I also use this method plus right-shift operation to get the leftest byte of an int.
You have jumped here from "when an int with of width four bytes" to
"so I can just" make that assignment for the desired result. The
problem with that is that for a random machine, int might not
have a width of -four- bytes. You have taken behaviour that is
true on one machine and generalized it to be true on all machines.
--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton
Walter Roberson wrote: In article <11************ **********@g49g 2000cwa.googleg roups.com>, What are "left" and "right" with respect to int? The byte order of int in a variable is unspecified, and the byte order of int in a CPU register might be different.
Yes, thank you. I'm a superficial guy with little knowledge. then Yes, a simple assignment is certain to have that result.
Thank you again, but it's not a homework.
lovecreatesbeau ty
"lovecreatesbea uty" <lo************ ***@gmail.com> writes: Walter Roberson wrote: In article <11************ **********@g49g 2000cwa.googleg roups.com>, What are "left" and "right" with respect to int? The byte order of int in a variable is unspecified, and the byte order of int in a CPU register might be different.
Yes, thank you. I'm a superficial guy with little knowledge.
then Yes, a simple assignment is certain to have that result.
Thank you again, but it's not a homework.
He meant "assignment " as in "x=y;", not as in "homework assignment".
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Eric Sosman <es*****@acm-dot-org.invalid> writes: lovecreatesbeau ty wrote: /* It seems that when an int with width of four bytes is assigned to a one byte width char, the first three bytes from left to right are discarded and the rightest byte is assigned to that char.
That's how it usually works out. From the language's point view you're assigning values rather than chunks of the representation, and the value stored in the char is derived from the value of the int via a conversion.
Your example code uses unsigned ints and chars, for which things will work as you describe. For signed variants the question of representation comes into play, and things could turn out differently on a system that doesn't use two's complement arithmetic. Such systems are surpassingly rare nowadays, but ...
The semantics of assigning a large value to an unsigned type too small
to hold it are well defined. The result is effectively to discard the
high-order bits (though it's defined in terms of the mathematical
values, not in terms of representation) .
Assigning an overly large value to a *signed* type yields an
implementation-defined result (or, in C99, raises an
implementation-defined signal). It typically discards the high-order
bits, but there are other possibilities; for example, saturation
(converting all large values to the maximum value of the target type)
is legal. You should usually just avoid doing that.
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Walter Roberson wrote: In article <11************ **********@g49g 2000cwa.googleg roups.com>, Your example is for "Win32", which (if I understand correctly) is an OS implemented only for instruction sets that happen not to use "bigendian" storage order in memory, and whose CPU registers are not necessarily the same as their memory storage order.
Thank you. I also tried this code snippet on a HP-UX 11i machine, the
result is:
$ cc +DAportable retrievebytefro mint.c
cc: "retrievebytefr omint.c", line 5: warning 27: Integer character
constant contains more than one character.
$ a.out
data: 0X6368756D
left: 0X63, c
right: 0X6D, m
$
lovecreatesbeau ty This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: mprocopio |
last post by:
Fellow JavaScripters,
I am looking for code or implementation ideas for converting an integer
variable to a four-byte array. I'm porting some of my code from C#,
where I make use of their BitConverter utility class, but am still
feeling my way around a bit here with JavaScript.
The idea is that each of the four bytes that comprise an integer (I
presume numeric types in JavaScript are/can be made four bytes?) are
extracted, and then...
|
by: Matt |
last post by:
I have 2 questions:
1. strlen returns an unsigned (size_t) quantity. Why is an unsigned
value more approprate than a signed value? Why is unsighned value less
appropriate?
2. Would there be any advantage in having strcat and strcpy return a
pointer to the "end" of the destination string rather than returning a
|
by: kj |
last post by:
How can one test if a pointer has been "freed" (i.e. with free())?
My naive assumption was that such a pointer would equal NULL, but
not so.
Thanks,
kj
--
NOTE: In my address everything before the first period is backwards;
|
by: QQ |
last post by:
I know a char is 2 bytes, the conversion is like
byte byte_array = new byte; //Allocate double mem as
that of char
then for each char do
byte = (byte) char & 0xff
byte = (byte)( char >> 8 & 0xff)
one unsigned char is 1 byte, could anyone tell me the conversion
method?
|
by: Winston Nimchan |
last post by:
Hi:
I'm currently developing a socket application and would like to precede the
data being sent with a 4 byte message length header (bin4). Can anyone help
Regards
Winston
| |
by: charles_gero |
last post by:
Hi Everyone,
I have a quick question regarding access to a file from disparate
CHAR_BIT systems. Has anyone had experience writing a file on a system
where CHAR_BIT is one value (let's use the value of 10) and then
reading said file from a system where this value is different (let's
say the common value of 8)?
I'm just curious how this would play out with respect to the standards,
etc. So for example, if I have a system where...
|
by: anon.asdf |
last post by:
Hi!
I want to assign the number 129 (binary 10000001) to the MSB (most
significant byte) of a 4-byte long and leave the other lower bytes in-
tact!
-working on normal pentium (...endian)
I want to do it with code that does NOT use shifts (<<) , bit-
operations (| &) !!
|
by: raphfrk |
last post by:
Is this valid?
int a;
void *b;
b = (void *)a; // b points to a
b += 5*sizeof(*a); // b points to a
a = 100;
|
by: Liang Chen |
last post by:
Hope you all had a nice weekend.
I have a question that I hope someone can help me out. I want to run a Python program that uses Tkinter for the user interface (GUI). The program allows me to type Chinese characters, but neverthelss is unable to show them up on screen. The follow is some of the error message I received after I logged off the program:
"Could not write output: <type "exceptions: UnicodeEncodeError'>, 'ascii' codec can't...
|
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...
|
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,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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,...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |