473,698 Members | 2,467 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 2698
jacob navia said:
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(unsigne d long long) == 8".

Please, are you a lawyer?

Can you tell me of a machine where char_bit != 8 ?
Assuming you mean CHAR_BIT, the use of 16- and even 32-bit bytes is
commonplace in modern digital signal processors. SHARC DSPs are an
obvious example.
And please, a machine in use 2007 ok?
It is not unlikely that you use several such processors yourself,
whether you realise it or not. DSPs are in common use in mobile phones,
for example.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 30 '07 #21
Mark Bluemel wrote:
Pietro Cerutti wrote:
>jacob navia wrote:
>>Pietro Cerutti wrote:

Richard Heathfield wrote:

... your
code invokes undefined behaviour.

Can you explain me this point, please?
At least, is it so under C99?
...
>>>
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?

My guess is that if Richard H says it invokes undefined behaviour, it
does. I'm a little disappointed that he has not felt fit to explain why
and give chapter and verse, but that's his perogative.
I agree on both your first and second sentence.
>
However, the undefined behaviour on the implementations you happen to
use (or indeed most or even all implementations ) may be for the code to
do what you expect.
Yes, I'm not assuming that my code is correct only because it gives the
result I was expecting..
--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp
Jul 30 '07 #22
Mark Bluemel wrote:
Pietro Cerutti wrote:
>jacob navia wrote:
>>Pietro Cerutti wrote:

Richard Heathfield wrote:

... your
code invokes undefined behaviour.

Can you explain me this point, please?
At least, is it so under C99?
...
>>>
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?

My guess is that if Richard H says it invokes undefined behaviour, it
does. I'm a little disappointed that he has not felt fit to explain why
and give chapter and verse, but that's his perogative.

However, the undefined behaviour on the implementations you happen to
use (or indeed most or even all implementations ) may be for the code to
do what you expect.
You agree with me then, that a shift right of 32 bits should give
you the upper 4 bytes ok?

I am getting nuts with all the language lawyers around.
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,
Typical legalese of lawyers's taste.

while Mr Navia will accept C code
working on the sample of machines he happens to work with.
What?

If sizeof(unsigned long long) is 8 and sizeof(int) is 4,
(and CHAR_BIT is 8)

unsigned long long >32 gives the upper 32 bits,
that can be safely stored into a 32 bit integer!

This in all machines as far as I can see.

By the way, I am using this code in
PowerPC (64 bits)
Solaris (Sparc)
64 bit windows
64 bit linux
32 bit windows
32 bit linux
[Disclaimer - I do not speak for either party here, and my comments
above are my interpreations of their positions]
OK, but I tell you: my position is that a right shift should do
it in ANY machine. You disagree with that???

You yourself you are unable to say why it should NOT work.
You have used (surely) this code.

Incredible, how lawyers scare people with legalese!
Jul 30 '07 #23
Jacob Navia wrote:
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(unsigne d 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?
cf. comp.arch.embed ded
Jul 30 '07 #24
Richard Heathfield wrote:
....
Here's a solution that I believe to be correct - I don't have a C99
compiler, so I can't check it (because of the unsigned long long) - but
corrections are most welcome:
Good job too.

[snip]
/* first ensure that a result is possible */
if(sizeof scnlen_lo < 4 || scnlen_hi < 4)
This should surely be:-

if(sizeof scnlen_lo < 4 ||sizeof scnlen_hi < 4)

And given the issues we meet later, I'd be inclined to add parentheses
(but then I'm often inclined to add parentheses):-

if((sizeof scnlen_lo < 4) ||(sizeof scnlen_hi < 4))

....
if(scnlen & mask <= UINT_MAX)
if((scnlen & mask) <= UINT_MAX)

....
if(scnlen & mask <= INT_MAX)
if((scnlen & mask) <= INT_MAX)

....
Jul 30 '07 #25
Richard Heathfield wrote:
Pietro Cerutti said:
>seema wrote:
>>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??
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.

If you meant those to be bit counts rather than byte counts, your code
invokes undefined behaviour.
You seem not to be willing to help me understanding why my code invokes
undefined behavior.
Maybe you're expecting some more work on my side, which I've done:
My source is n1124.

As of "6.5.7", shift operators invoke undefined only if the right
operand is negative (not the case in my code) or greater than the width
of the left operand (not the case in my code).
Moreover, the left shift operator (which I do not use) invokes undefined
behavior if the result is not representable in the result type.

As of "6.5.10", the bitwise AND operator never causes undefined behavior.

So, please, could you point me to where the problem is?
Thank you in advance.

--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp
Jul 30 '07 #26
Mark Bluemel said:

<snip>
My guess is that if Richard H says it invokes undefined behaviour, it
does.
That's a good guess, but on this occasion you're mistaken. The code does
not in fact invoke undefined behaviour, and I was mistaken to claim
otherwise. I therefore owe apologies to Mr Cerutti and Mr Navia.
I'm a little disappointed that he has not felt fit to explain
why and give chapter and verse, but that's his perogative.
My reasoning (which would have worked just fine if I'd been right about
the UB) was that Mr Navia should have been perfectly capable of working
out the answer for himself and announcing it by himself. This turns out
to be impossible, of course, because I was mistaken in my claim, and
therefore incorrectly "corrected" a more or less correct post, in which
there was no UB to spot.

Now that I've taken my lumps, I'll explain what I thought was wrong. It
was the right shift by 32 bits. This is *not* undefined behaviour
because it is performed on an integer that is guaranteed to occupy more
than 32 bits (at least 64, in fact).

To make matters worse, I checked the code *twice* after spotting the
"bug" (as I thought it was), before posting my "correction ".

The assumptions by both Mr Navia and Mr Cerutti that bytes are always 8
bits wide, whilst incorrect, pale in comparison with my own blunder.

<snip>

And now I suppose it's time for the clowns to have their field day. Pace
regs - I shall not be reading their responses.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 30 '07 #27
Mark Bluemel said:
Richard Heathfield wrote:
...
>Here's a solution that I believe to be correct - I don't have a C99
compiler, so I can't check it (because of the unsigned long long) -
but corrections are most welcome:

Good job too.
<Three corrections snipped>

Today is not being a good day, is it?

Thank you for the corrections.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 30 '07 #28
Richard Heathfield <rj*@see.sig.in validwrites:
Pietro Cerutti said:
>seema wrote:
>>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??

sclen_hi = scnlen >32;
sclen_lo = scnlen & 0xFFFFFFFF;

Assuming that sizeof(unsigned long long) == 64 and sizeof(int) == 32

If you meant those to be bit counts rather than byte counts, your code
invokes undefined behaviour.
Let me expand on this. Consider the first assignment:
sclen_hi = scnlen >32;
scnlen has type "unsigned long long", which is at least 64 bits
wide. Thus, shifting it right by a mere 32 bits is
well-defined. The result is then assigned to sclen_hi, which has
type "unsigned int". Regardless of whether "unsigned int" is
large enough to hold 32 bits, the assignment is acceptable: any
high bits will simply be trimmed off.

Consider the second assignment:
sclen_lo = scnlen & 0xFFFFFFFF;
I believe that 0xFFFFFFFF has type "int", "unsigned int",
"unsigned long int", or "unsigned long long int", depending.
Regardless of its type, the "&" operation against scnlen will
cause it to be promoted to "unsigned long long int". The result
is assigned to sclen_lo.

The possibility for undefined behavior seems to be in the range
of "int", which is the type of sclen_lo. If "int" is 32 bits,
then converting a large 32-bit unsigned integer to "int" invokes
undefined behavior, because it is outside the range of "int". I
imagine that this is what Richard is talking about.

Another possibility is that Richard is concerned about the
possibility of padding bits in various types. Padding bits are
interesting theoretically but rarely come up in real life, so I'm
not going to go any further in this direction.

The example number of 888888 would not yield undefined behavior,
as far as I can see. I don't think that's the issue at hand.
--
"Some programming practices beg for errors;
this one is like calling an 800 number
and having errors delivered to your door."
--Steve McConnell
Jul 30 '07 #29
jacob navia wrote:
Mark Bluemel wrote:
>>
My guess is that if Richard H says it invokes undefined behaviour, it
does. I'm a little disappointed that he has not felt fit to explain
why and give chapter and verse, but that's his perogative.

However, the undefined behaviour on the implementations you happen to
use (or indeed most or even all implementations ) may be for the code
to do what you expect.

You agree with me then, that a shift right of 32 bits should give
you the upper 4 bytes ok?
Given the full set of constraints - 8-bit bytes, 64-bit unsigned long
longs, 32-bit signed and unsigned ints - yes, I agree it should work.
However, I admit that my understanding is not complete, especially when
we look at the fine detail of the language specification.

As I said above, if Richard H says that this invokes undefined
behaviour, based on my experiences of his postings I'm inclined to
believe him, though I'd rather like to see chapter and verse.
>
I am getting nuts with all the language lawyers around.
>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,

Typical legalese of lawyers's taste.
This is not legalese - or at least I did not intend it to be so. I was
trying to accurately characterise Richard H's position, which is a
purist position relying only on what the language specification asserts.
>while Mr Navia will accept C code
working on the sample of machines he happens to work with.


What?

If sizeof(unsigned long long) is 8 and sizeof(int) is 4,
(and CHAR_BIT is 8)

unsigned long long >32 gives the upper 32 bits,
that can be safely stored into a 32 bit integer!

This in all machines as far as I can see.

By the way, I am using this code in
PowerPC (64 bits)
Solaris (Sparc)
64 bit windows
64 bit linux
32 bit windows
32 bit linux
This is the point where we need to wheel out the DS9K I suspect.

Other posters have already pointed out platforms where some of the
initial constraints are broken.
>
>[Disclaimer - I do not speak for either party here, and my comments
above are my interpreations of their positions]


OK, but I tell you: my position is that a right shift should do
it in ANY machine. You disagree with that???
You yourself you are unable to say why it should NOT work.
I have stated above that I recognise that my knowledge is limited, and
I'm more inclined to trust Richard's informed judgement than mine on
this point.
You have used (surely) this code.
Code I work with uses such constructions, but it is code which is
regarded as platform-specific, not as totally portable.
Jul 30 '07 #30

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

Similar topics

25
6709
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
1928
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
3271
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
17741
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
1292
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
6255
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
7068
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
8678
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, 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...
0
8609
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
9030
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8871
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...
1
6525
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 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...
0
5861
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
4371
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...
1
3052
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
2333
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.