473,698 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Shifting unsigned long long values by 64 bits

Hi,

I am trying to shift unsigned long long value by 64 bits and this is
what i get

#include <stdio.h>

int main()
{
unsigned short shiftby= 64;
fprintf(stderr, "Value (using hardcoded 64) : %llx\n", \
(((unsigned long long) ~0ULL) << 64));
fprintf(stderr, "Value (w/o hardcoded 64) : %llx\n", \
(((unsigned long long) ~0ULL) << shiftby));
}

gcc file.c
t2.c: In function `main':
t2.c:7: warning: left shift count >= width of type
<IMPORTANT>
Value (using hardcoded 64) : 0
Value (w/o hardcoded 64) : fffffffffffffff f
</IMPORTANT>

Why is the behavior different if we try to shift value by 64 bits using
a variable
as against direct numeric "64"?

Regards,
Krunal

Jan 22 '07 #1
10 10691

"krunalb" <kr***********@ gmail.comwrote in message
news:11******** *************@1 1g2000cwr.googl egroups.com...
Hi,

I am trying to shift unsigned long long value by 64 bits and this is
what i get

#include <stdio.h>

int main()
{
unsigned short shiftby= 64;
fprintf(stderr, "Value (using hardcoded 64) : %llx\n", \
(((unsigned long long) ~0ULL) << 64));
fprintf(stderr, "Value (w/o hardcoded 64) : %llx\n", \
(((unsigned long long) ~0ULL) << shiftby));
}

gcc file.c
t2.c: In function `main':
t2.c:7: warning: left shift count >= width of type
<IMPORTANT>
Value (using hardcoded 64) : 0
Value (w/o hardcoded 64) : fffffffffffffff f
</IMPORTANT>

Why is the behavior different if we try to shift value by 64 bits using
a variable
as against direct numeric "64"?

Regards,
Krunal
Its undefined behaviour.
Jan 22 '07 #2
krunalb wrote:
Hi,

I am trying to shift unsigned long long value by 64 bits and this is
what i get

#include <stdio.h>

int main()
{
unsigned short shiftby= 64;
fprintf(stderr, "Value (using hardcoded 64) : %llx\n", \
(((unsigned long long) ~0ULL) << 64));
fprintf(stderr, "Value (w/o hardcoded 64) : %llx\n", \
(((unsigned long long) ~0ULL) << shiftby));
}

gcc file.c
t2.c: In function `main':
t2.c:7: warning: left shift count >= width of type
<IMPORTANT>
Value (using hardcoded 64) : 0
Value (w/o hardcoded 64) : fffffffffffffff f
</IMPORTANT>

Why is the behavior different if we try to shift value by 64 bits using
a variable
as against direct numeric "64"?
Quoth ISO/IEC 9899:1999:

6.5.7 Bitwise Shift Operators
[...]
3 [...] If the value of the right operand is negative or is
greater than or equal to the width of the promoted
left operand, the behavior is undefined.
Mark F. Haigh
mf*****@sbcglob al.net

Jan 22 '07 #3

krunalb wrote:
Hi,

I am trying to shift unsigned long long value by 64 bits and this is
what i get

#include <stdio.h>

int main()
{
unsigned short shiftby= 64;
fprintf(stderr, "Value (using hardcoded 64) : %llx\n", \
(((unsigned long long) ~0ULL) << 64));
fprintf(stderr, "Value (w/o hardcoded 64) : %llx\n", \
(((unsigned long long) ~0ULL) << shiftby));
}

gcc file.c
t2.c: In function `main':
t2.c:7: warning: left shift count >= width of type
<IMPORTANT>
Value (using hardcoded 64) : 0
Value (w/o hardcoded 64) : fffffffffffffff f
</IMPORTANT>

Why is the behavior different if we try to shift value by 64 bits using
a variable as against direct numeric "64"?
It isn't on my AIX system, using the xlc compiler...

It's undefined behaviour, the compiler is free to do what it likes and
the compiler doesn't have to be consistent...

Jan 22 '07 #4
krunalb wrote:
>
Hi,

I am trying to shift unsigned long long value by 64 bits and this is
what i get

#include <stdio.h>

int main()
{
unsigned short shiftby= 64;
fprintf(stderr, "Value (using hardcoded 64) : %llx\n", \
(((unsigned long long) ~0ULL) << 64));
fprintf(stderr, "Value (w/o hardcoded 64) : %llx\n", \
(((unsigned long long) ~0ULL) << shiftby));
}

gcc file.c
t2.c: In function `main':
t2.c:7: warning: left shift count >= width of type

<IMPORTANT>
Value (using hardcoded 64) : 0
Value (w/o hardcoded 64) : fffffffffffffff f
</IMPORTANT>

Why is the behavior different if we try to shift value by 64 bits using
a variable as against direct numeric "64"?
As mentioned elsewhere, this is UB. However, this may explain the
symptoms of UB that you are seeing on your particular platform. This
is, of course, pure conjecture based on the observed results.

1 - The compiler sees the hard-coded 64 bit shift and, knowing that
the value is only 64 bits to begin with, knows the result would
be zero, and compiles with a constant zero as the parameter.
(Or, perhaps, actually did the caluclation, as both values are
constants. In which case, it came up with the "expected" value
of zero.)

Note, too, the compiler warning for this line.

2 - When shifting by "shiftby" instead, it needs to compile code to
shift at the machine-code level, and places "64" in a register,
and executes an "unsigned left shift" of that amount. The CPU,
knowing that the operation is working on 64-bit values, only
uses the lower 6 bits of the shift amount. In this case, that
value is zero.

I ran into (2) recently on code which worked just fine on a platform
which supported 64-bit ints, but failed on a system which only supported
32-bit ints. In my case, I was splitting a value into two 32-bit values
to pass as parameters to a function. I did this by right-shifting the
value by 32 bits to get the high-order 32 bits. On the 64-bit-aware
platform, this worked just fine. On the 32-bit-only platform, the
shift of 32 bits was, at the CPU level, treated as a zero-bit shift
(the low-order 5 bits of the shift value being zero), causing the wrong
value for the "high 32 bits" parameter to be passed. (It should, of
course, been zero, as there were only the low-order 32 bits in the
value to begin with. Instead, it duplicated the low-order bits as
the high-order bits.)

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>

Jan 22 '07 #5
"Kenneth Brody" <ke******@spamc op.netwrote in message
news:45******** *******@spamcop .net...
>
As mentioned elsewhere, this is UB. However, this may explain the
symptoms of UB that you are seeing on your particular platform. This
is, of course, pure conjecture based on the observed results.

1 - The compiler sees the hard-coded 64 bit shift and, knowing that
the value is only 64 bits to begin with, knows the result would
be zero, and compiles with a constant zero as the parameter.
(Or, perhaps, actually did the caluclation, as both values are
constants. In which case, it came up with the "expected" value
of zero.)

Note, too, the compiler warning for this line.

2 - When shifting by "shiftby" instead, it needs to compile code to
shift at the machine-code level, and places "64" in a register,
and executes an "unsigned left shift" of that amount. The CPU,
knowing that the operation is working on 64-bit values, only
uses the lower 6 bits of the shift amount. In this case, that
value is zero.

I ran into (2) recently on code which worked just fine on a platform
which supported 64-bit ints, but failed on a system which only supported
32-bit ints. In my case, I was splitting a value into two 32-bit values
to pass as parameters to a function. I did this by right-shifting the
value by 32 bits to get the high-order 32 bits. On the 64-bit-aware
platform, this worked just fine. On the 32-bit-only platform, the
shift of 32 bits was, at the CPU level, treated as a zero-bit shift
(the low-order 5 bits of the shift value being zero), causing the wrong
value for the "high 32 bits" parameter to be passed. (It should, of
course, been zero, as there were only the low-order 32 bits in the
value to begin with. Instead, it duplicated the low-order bits as
the high-order bits.)
Wow! I've never seen this behavior. I would have assumed that shifting
something too many times to the left always gets you zero and to the right
either gets you zero or -1.

Wow!

--
David T. Ashley (dt*@e3ft.com)
http://www.e3ft.com (Consulting Home Page)
http://www.dtashley.com (Personal Home Page)
http://gpl.e3ft.com (GPL Publications and Projects)
Jan 22 '07 #6
krunalb wrote:
>
I am trying to shift unsigned long long value by 64 bits and this
is what i get
Try thinking a moment. If you shift off 64 bits of a 64 bit
quantity, what do you have left?

--
<http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
Jan 22 '07 #7
CBFalconer <cb********@yah oo.comwrites:

nkrunalb wrote:
>>
I am trying to shift unsigned long long value by 64 bits and this
is what i get

Try thinking a moment. If you shift off 64 bits of a 64 bit
quantity, what do you have left?
Undefined behavior.
--
"When in doubt, treat ``feature'' as a pejorative.
(Think of a hundred-bladed Swiss army knife.)"
--Kernighan and Plauger, _Software Tools_
Jan 22 '07 #8
David T. Ashley wrote:
Wow! I've never seen this behavior. I would have assumed that shifting
something too many times to the left always gets you zero and to the right
either gets you zero or -1.
On IA-32 processors (Pentium, Athlon etc. ), a hardware shift
instruction uses only the lower five bit of the shift count, so (x <<
n) produces the same result whether n == 32 or n == 0.

Jan 22 '07 #9
In article <0t************ *************** ***@giganews.co m"David T. Ashley" <dt*@e3ft.comwr ites:
....
Wow! I've never seen this behavior. I would have assumed that shifting
something too many times to the left always gets you zero and to the right
either gets you zero or -1.
There are quite a few processors that use only the lower bits of the
bit count if it is in a register. Moreover, there are also processors
that do not have an arithmetic right shift. That is the reason that
the standard states that:
(1) Shifting by the width of the type or more is undefined.
(2) Shifting a negative number to the right gives implementation-defined
result.
So (assuming a width of 32 for i, and i < 0):
(i >16) >16
can indeed yield -1 (2-s complement arithmetic shift), -0 (1-s complement
arithmetic shift), -2147483647 (sign-magnitude arithmetic shift) or 0
(logical shift)). (But I do not know whether sign-magnitude is actually
allowed.)
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Jan 23 '07 #10

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

Similar topics

7
3601
by: William Payne | last post by:
Hello, I have a variable of type unsigned long. It has a number of bits set (with set I mean they equal one). I need to determine those bits and their position and create new numbers from them. For example, consider this four-bit number: 1100 from this number I want to extract two numbers: 1000 and 100 had the four-bit number been 0101 I would want to extract 100 and 1. How should I do this? I wish I had some code to post but I don't...
8
3667
by: ben | last post by:
i have a bit of code, that works absolutely fine as is, but seems over complicated/long winded. is there anyway to shorten/simplify it? the code is below. description of it: it's like strcpy in that it copies one block of data to another block of data until the block that is being copied contains a zero/null. the difference with this code is that it's doing 4bits at a time (all the values are 4bits) and the two blocks of data may not be...
25
7917
by: Allan Rydberg | last post by:
hi i'm trying to shift a double, but i'm getting the error message '>>' illegal, left operand double. althought that the manpages say, '>>' and '<<' can be applied for int's only, i was able to use the shift operator on unsigned longs without any problems. does anyone know how to do this properly?
6
2886
by: digi.empire | last post by:
trying to wrap my mind around these two exercises: 1. given an int (which is 16 bits in c), if the 16 bits are: A15A14A13A12A11A10A9A8A7A6A5A4A3A2A1A0 it will output an int in whose 16 bits are: A7A6A5A4A3A2A1A0A15A14A13A12A11A10A9A8
20
2506
by: Charles Sullivan | last post by:
I understand different processor hardware may store the bits in a byte in different order. Does it make a difference in C insofar as bit-shifting unsigned char variables is concerned? E.g, if I have unsigned char x = 1; is it always true that (x << 1) == 2 (x << 2) == 4 etc?
2
2272
by: johnno | last post by:
Is anyone able to help me here? I have the following VB code and wish to have it rewritten in C++ but unsure how. Any help would be greatly appreciated. Effectively the code is packing a 10 letter word into 60 bits. I only need to deal with 64 valid values. ie the values 90 - 64gv so the first 26 values are not needed. Thanks in advance Public Function Pack(ByVal TheData As String) As String Dim c As Double Dim x As Long
7
5045
by: somenath | last post by:
Hi All, I am trying to undestand "Type Conversions" from K&R book.I am not able to understand the bellow mentioned text "Conversion rules are more complicated when unsigned operands are involved. The problem is that comparisons between signed and unsigned values are machine- dependent, because they depend on the sizes of the various integer types. For example, suppose that int is 16 bits
30
4890
by: viza | last post by:
Hi all int i= INT_MIN; unsigned int u= -i; Is u guaranteed to have the absolute value of INT_MIN? Why it might not: -i has type (int), and -INT_MIN might be more than INT_MAX.
8
1548
by: manu | last post by:
Hi All, I have executed the below program and got 0x1f as output... Can anyone explain me why this output is coming instead of zero? Compiler - gcc int main() {
0
9169
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
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...
1
8899
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,...
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
6528
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...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2335
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.