473,569 Members | 2,400 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 #1
49 2678
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??

Thanks in advance,
Seema Rao
Assuming sizeof(int)==4 (32 bits)

scnlen_hi=(scnl en>>32);
scnlen_lo=(scnl en&0xffffffff) ;
Jul 30 '07 #2
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
>
Thanks in advance,
Seema Rao


--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp
Jul 30 '07 #3
jacob navia <ja***@jacob.re mcomp.frwrites:
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??

Thanks in advance,
Seema Rao

Assuming sizeof(int)==4 (32 bits)

scnlen_hi=(scnl en>>32);
scnlen_lo=(scnl en&0xffffffff) ;
Assuming sizeof int ==4 then scnlen>>32 doesnt give the top 4 bytes.

Did he mean 2 bytes?

Or should we be assuming 64 bit ints in which case you are right.
Jul 30 '07 #4
jacob navia 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??
Assuming sizeof(int)==4 (32 bits)

scnlen_hi=(scnl en>>32);
scnlen_lo=(scnl en&0xffffffff) ;
Wrong. Your "solution" invokes undefined behaviour.

--
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 #5
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.

--
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 #6
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".
Jul 30 '07 #7
Richard Heathfield wrote:
jacob navia 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??

Assuming sizeof(int)==4 (32 bits)

scnlen_hi=(sc nlen>>32);
scnlen_lo=(sc nlen&0xffffffff );


Wrong. Your "solution" invokes undefined behaviour.
Would it do anyone any harm if you were to a) explain why this is the
case, and b) explain how to solve the original poster's problem correctly?
Jul 30 '07 #8
Mark Bluemel <ma**********@p obox.comwrites:
Richard Heathfield wrote:
>jacob navia 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??
Assuming sizeof(int)==4 (32 bits)

scnlen_hi=(s cnlen>>32);
scnlen_lo=(s cnlen&0xfffffff f);


Wrong. Your "solution" invokes undefined behaviour.
Would it do anyone any harm if you were to a) explain why this is the
case, and b) explain how to solve the original poster's problem
correctly?
"Mr" Heathfield is having a hissy fit because he doesn't feel he is
getting the appreciation and the kudos to the level at which he thinks
he deserves.
Jul 30 '07 #9
Mark Bluemel said:
Richard Heathfield wrote:
>jacob navia 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??
Assuming sizeof(int)==4 (32 bits)

scnlen_hi=(s cnlen>>32);
scnlen_lo=(s cnlen&0xfffffff f);


Wrong. Your "solution" invokes undefined behaviour.
Would it do anyone any harm if you were to a) explain why this is the
case, and b) explain how to solve the original poster's problem
correctly?
I'll let Mr Navia explain why he's wrong.

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:

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int rc = EXIT_SUCCESS;
unsigned int scnlen_lo;
int scnlen_hi;
unsigned long long scnlen;
scnlen = 888888ULL;

/* first ensure that a result is possible */
if(sizeof scnlen_lo < 4 || scnlen_hi < 4)
{
puts("No solution exists.\n");
rc = EXIT_FAILURE;
}
else
{
/* build a mask with all bits set */
unsigned long long mask = -1;

/* we need to shift the mask leftward by 4 bytes,
i.e. 4 * CHAR_BIT */
size_t n = CHAR_BIT;
while(n--)
{
mask <<= 4;
}
/* now we can flip */
mask = ~mask;

/* that gives us half the solution with a simple & */
if(scnlen & mask <= UINT_MAX)
{
scnlen_lo = scnlen & mask;
}
else
{
puts("No solution exists for the low four bytes");
rc = EXIT_FAILURE;
}

/* now we need to go the other way */
mask = -1;
n = CHAR_BIT;
while(n--)
{
mask >>= 4;
}
/* same ol' same ol' */
mask = ~mask;
if(scnlen & mask <= INT_MAX)
{
scnlen_hi = scnlen & mask;
}
else
{
puts("No solution exists for the high four bytes");
rc = EXIT_FAILURE;
}
}
return rc;
}

--
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 #10

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

Similar topics

25
6658
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...
8
1923
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
3262
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,...
12
17729
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
1291
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...
5
6229
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
7057
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
7618
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...
0
8138
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...
1
7679
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...
0
7983
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...
0
6287
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...
1
5514
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...
0
5223
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...
0
3657
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...
0
946
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...

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.