473,738 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

bytes calculation

Hi all,

How to calculate upper 4 bytes and lower 4 bytes of a value? Can
somebody please explain??
say for example,
unsigned int scnlen_lo;
int scnlen_hi;
unsigned long long scnlen;

scnlen=888888;
scnlen_hi= ??//how to calculate upper 4 bytes of scnlen?
scnlen_lo =?? //how to calculate lower 4 bytes of scnlen??

Thanks in advance,
Seema Rao

Jul 30 '07
49 2714
[snips]

On Mon, 30 Jul 2007 18:04:40 +0200, Richard wrote:
FWIW, there is nothing wrong with making mistakes. There is something
very wrong, however, when sneering at people and acting like a little
princess and being wrong all in one.
People who should know better have been ragging him repeatedly for giving
correct and helpful answers, even if ones that may require a little noodle
work on their part. Why should he *not* give these people a hearty "Screw
you if you don't want help"?

He should. He has. Such people are contemptible.
Jul 30 '07 #41
On Mon, 30 Jul 2007 16:55:28 +0200, jacob navia
<ja***@jacob.re mcomp.frwrote in comp.lang.c:
Spoon wrote:
Pietro Cerutti wrote:
Assuming that sizeof(unsigned long long) == 64 and sizeof(int) == 32
I think you meant "Assuming CHAR_BIT == 8, sizeof(int) == 4, and
sizeof(unsigned long long) == 8".

Please, are you a lawyer?

Can you tell me of a machine where char_bit != 8 ?

And please, a machine in use 2007 ok?
Sure, the Texas Instruments TMS320F2812. We have it on two different
boards in a safety-cricitcal medical device. I have written quite a
bit of code for it, and will be writing some more later this week.

CHAR_BIT = 16
sizeof (char) is 1
sizeof (short) is 1
sizeof (int) is 1
sizeof (long) is 2
sizeof (long) long is 4
sizeof (void *) is 2

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Jul 31 '07 #42
/* first ensure that a result is possible */
if(sizeof scnlen_lo < 4 || scnlen_hi < 4)
{
is the test "scnlen_hi < 4)" valid ? it may always fail

Jul 31 '07 #43
somenath wrote:
> /* first ensure that a result is possible */
if(sizeof scnlen_lo < 4 || scnlen_hi < 4)
{

is the test "scnlen_hi < 4)" valid ? it may always fail
See my corrections to this code. It is (or should be) fairly obvious
what's missing here.
Jul 31 '07 #44
On Mon, 30 Jul 2007 17:08:53 +0200, jacob navia wrote:
Pietro Cerutti wrote:
>jacob navia wrote:
>>Pietro Cerutti wrote:
Richard Heathfield wrote:
Pietro Cerutti said:
>
>seema wrote:
>>Hi all,
>>>
>>How to calculate upper 4 bytes and lower 4 bytes of a value? Can
>>somebod y please explain??
>>say for example,
>> unsigned int scnlen_lo;
>> int scnlen_hi;
>>unsigne d long long scnlen;
>>>
>> scnlen=888888;
>> scnlen_hi= ??//how to calculate upper 4 bytes of scnlen?
>> scnlen_lo =?? //how to calculate lower 4 bytes of scnlen??
>sclen_hi = scnlen >32;
>sclen_lo = scnlen & 0xFFFFFFFF;
>>
>Assuming that sizeof(unsigned long long) == 64 and sizeof(int) == 32
Under those assumptions and assuming CHAR_BIT is 8, sclen_hi now
actually contains the top 28 bytes of scnlen, which isn't what was
asked.
Yes, sorry. I meant sizeof(unsigned long long) == 8, and sizeof(int) == 4

If you meant those to be bit counts rather than byte counts, your
code invokes undefined behaviour.
Can you explain me this point, please?
At least, is it so under C99?

cat ctemp.c
#include <stdio.h>

int main(void)
{
/**** START ****/
int hi, lo;
unsigned long long full;

full = 0xaaaaaaaabbbbb bbb;

hi = full >32;
lo = full & 0xFFFFFFFF;

printf("full is %llx\n", full);
printf("hi is %x\n", hi);
printf("low is %x\n", lo);

/**** STOP ****/
return(0);
}

gcc -Wall -std=c99 -o ctemp ctemp.c && ./ctemp
full is aaaaaaaabbbbbbb b
hi is aaaaaaaa
low is bbbbbbbb
Please do not believe everything heathfield says...

He is a language lawyer, and those people do not live by the
same rules as the rest of us

:-)

I can't get the point.
Does my code invokes undefined behavior or not?

I do not see any. It works in linux/windows/Mac/and many others.
But heathfield sees something, I do not know what, he is the lawyer,
not me!
Even if int has 32 bits and no padding, 0xaaaaaaaa doesn't fit in
it, and will become a negative value. But the specifier %x is for
unsigned values.
This happens to work on two's complement machines where signed int
overflow silently wrap around (so essentially the only difference
between signed and unsigned is the *meaning*, not the behavior, of
representations with the most significant bit set). But as per the
Standard, signed integer overflow is UB. (This is easily fixed by
declaring hi and lo as unsigned, or better as unsigned long (and
using "%lx") which is required to have at least 32 bits.)
--
Army1987 (Replace "NOSPAM" with "email")
"Never attribute to malice that which can be adequately explained
by stupidity." -- R. J. Hanlon (?)

Aug 1 '07 #45
Pietro Cerutti wrote:
jacob navia wrote:
>Pietro Cerutti wrote:
.... snip ...
>>
>>#include <stdio.h>

int main(void) {
/**** START ****/
int hi, lo;
unsigned long long full;

full = 0xaaaaaaaabbbbb bbb;
hi = full >32;
lo = full & 0xFFFFFFFF;

printf("full is %llx\n", full);
printf("hi is %x\n", hi);
printf("low is %x\n", lo);

/**** STOP ****/
return(0);
}

full is aaaaaaaabbbbbbb b
hi is aaaaaaaa
low is bbbbbbbb

Please do not believe everything heathfield says...
....
>
I can't get the point.
Does my code invokes undefined behavior or not?
Yes. Nothing enforces CHAR_BIT == 8 for one thing. Then you have
unbandied use of ints to hold unsigned values. etc.

--
<http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfoc us.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Aug 1 '07 #46
Mark Bluemel wrote:
>
.... snip ...
>
Here we enter into the realms of philosophy and personal taste - Mr
Heathfield wishes for all C code to be provably correct (loosely
speaking) for a general abstract C implementation which sticks to
the letter of the language specification, while Mr Navia will
accept C code working on the sample of machines he happens to work
with. [Disclaimer - I do not speak for either party here, and my
comments above are my interpreations of their positions]
However, RH's code is much more likely to work on any machine than
JNs. If RH wishes to use machine specific code he can do so, and
isolate it in easily replaced modules. Believe it or not, there
is no extra effort involved in doing this. If does, however,
require knowing the restrictions and limitations placed on C by the
standard.

--
<http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfoc us.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net
--
Posted via a free Usenet account from http://www.teranews.com

Aug 1 '07 #47
seema wrote:
>
How to calculate upper 4 bytes and lower 4 bytes of a value? Can
somebody please explain??
say for example,
unsigned int scnlen_lo;
int scnlen_hi;
unsigned long long scnlen;

scnlen=888888;
scnlen_hi= ??//how to calculate upper 4 bytes of scnlen?
scnlen_lo =?? //how to calculate lower 4 bytes of scnlen??
Why do you even conceive that sclen_hi (or lo) contains 4 bytes?

You can usually use the modulo and division instructions to do this
sort of isolation.

--
<http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfoc us.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Aug 1 '07 #48
CBFalconer wrote:
However, RH's code is much more likely to work on any machine than
JNs.
I am amazed at your powers.

Without ever seeing a line of code from me, you are able to
assert that my code is much more likely to work
in any machine than RH's

http://dictionary.reference.com/browse/prejudice

prejudice
Pronunciation[prej-uh-dis]
–noun
1. an unfavorable opinion or feeling formed beforehand or without
knowledge, thought, or reason.
2. any preconceived opinion or feeling, either favorable or unfavorable.
Aug 1 '07 #49
jacob navia wrote:
CBFalconer wrote:
>However, RH's code is much more likely to work on any machine than
JNs.

I am amazed at your powers.

Without ever seeing a line of code from me, you are able to
assert that my code is much more likely to work
in any machine than RH's
Not your code, Richard H's.

[snip]

Aug 1 '07 #50

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

Similar topics

25
6737
by: TK | last post by:
I'm used to programming in c or c++ in which my problem is simple. I want to be able to enter a value on a page (like 3.2), and then read it as a 32-bit float and break it into it's individual bytes. I've tried using bitwise operators, but they seem to convert the value into an integer first, and i've tried using the toString() method to convert it into a hex value so i can parse it, but that also seems to first convert it into an...
8
1931
by: ranjeet.gupta | last post by:
Dear All I am not able o understand the exact number of bytes allocation done by the two fucntion given below, It is said that the fuction Condition_String1 allocates the 240 bytes while Condition_String2 allocates the 72 bytes, I am not able to get the Artimatic Numbers to staisfy the above Number of bytes allocated,
4
3277
by: Michiel Alsters | last post by:
Hello everybody, I hope anybody can help me. I'll try to give a brief overview of my problem. I have running a program that performs a heavy calculation. To give the user feedback what the program is doing I show a window which contains a progress bar and a label. At some point during the execution the state of the calculation is changed, so I want to let the user know this. I have placed the creation of the form in a seperate thread...
12
17748
by: PiotrKolodziej | last post by:
Hi I have long variable containing number of stored bytes. I want to display Bytes , KB, MB, GB depending of the size of this variable as a string. Does framework provide any class for such a fast calculation or i have to divide by 1024, check if result is zero, if not divide again and so on...? Thanks PK
0
1293
by: hiitsmedear | last post by:
Hi, I am working on memory leaks for a windows service and the way i am calculating the leaks is appears tobe somewhat weired to me, Can someone please suggest me that is this the correct way to do the calculation or some other alternate way available to do the same. I run the service for 10 hours(H). I log the Private bytes using Perfmon. than i extract the maximum(X) and minimum(N) of Private bytes from the Log.
5
6258
by: The alMIGHTY N | last post by:
Hi all, Let's say I have a simple math formula: sum (x * y / 1000) / (sum z / 1000) I have to do this across 50 items, each with an x, y and z value, when the page first loads AND when a user modifies any of the x, y and z values.
9
7070
by: samuraisam | last post by:
Quick file size formatting for all those seekers out there... import math def filesizeformat(bytes, precision=2): """Returns a humanized string for a given amount of bytes""" bytes = int(bytes) if bytes is 0: return '0bytes' log = math.floor(math.log(bytes, 1024))
0
8787
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,...
0
9473
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, 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...
0
9208
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
8208
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
6053
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();...
0
4824
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3279
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
2744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
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.