-4

I am trying to do left shift greater then or equal to 64, but I am not sure which DATA TYPE exists to help me out here.

I'm working on existing project, where 63 macros are already taken the next comes 64(which are my changes) for which i have to do left shift operation.

Note: I just want to understand how do i set a particular bit greater then 64bits. "I am not sure which DATA TYPE exists to help me out here". Below code is just a sample code. We know there no data type exists greater then 64bits, but can there be any solution for this.

#include <stdio.h>
#define PEAK 64

int main()
{
  unsigned long int a;
  a= (1ULL << PEAK);
  printf("%lu",a);
  return 0;
}

main.c:8:10: warning: left shift count >= width of type [-Wshift-count-overflow]
a= (1ULL << PEAK);
^~

2 Answers2

2

I just want to understand how do i set a particular bit greater then 64bits.

You can't.

Old answer:

You can do a left shift greater than or equal to 64-bits by doing exactly what you're doing.

This, of course, won't result in anything usable (either the original value, zero, or something else), and is undefined behavior, so don't do it.

If you want a data type that can do this, you're mostly out of luck. There are no guarantees that an 128-bit data type exists in C, and any compiler extensions that you may see are not portable. This may be possible with SIMD instructions but they're not portable across processors.

That said, there is unsigned __int128 in GCC and Clang that allows shifting (through emulation of wider integers). However, this isn't available in MSVC. Also note that you won't be able to print this number, so it's pretty pointless anyway.

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
0

You can shift a 64-bit unsigned type left by zero to 63 bits. Anything else will lead to undefined behaviour. The largest unsigned integer type is uintmax_t but it is usually unsigned long long on most common implementations, which is 64 bits, and the shift is equally undefined then. In practice it will result in either zero, the original value, or completely random behaviour.

Why do you think you need to do this?

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
  • 1
    This does not answer the question. – Eric Postpischil Aug 20 '19 at 10:25
  • 1
    @EricPostpischil It does answer the question "how do I do left-shift greater than 64 bits" by saying "you don't do that by simply using a bitshift on an unsigned integer type". There might be other answers that you're very welcome to post. – Antti Haapala -- Слава Україні Aug 20 '19 at 10:34
  • (a) GCC and Clang both offer `unsigned __int128`. (b) Computations with shifts exceeding 64 bits can be effected by emulating wider integers. In other words, we can write programs that compute desired results by building them out of various fundamental operations. (c) The possibility of entering other answers does not render this answer useful or correct, so it is not relevant to evaluating this answer. (d) Behavior not defined by the C standard is not random behavior. – Eric Postpischil Aug 20 '19 at 11:06
  • 1
    Behaviour not defined by C standard can for all intents and purposes be totally random, to the extent that even the author of the compiler implementation might not be able to predict what the behaviour would be in any instance without compiling the program. – Antti Haapala -- Слава Україні Aug 20 '19 at 11:23
  • There exist some situations in which behavior not defined by the C standard can be effectively random, as when effects are subject to address space layout randomization. But behavior not defined by the C standard is not generally random. Random means a lack of pattern or predictability, but much behavior not defined by the C standard is a consequence of things other than the standard, such as the designs and behaviors of compilers, operating systems, machines, and such. These things give the behavior some amount of pattern and predictability, so it is not, for all intents and purposes, random. – Eric Postpischil Aug 20 '19 at 11:29
  • In particular, observations of program behavior often give clues as to what went wrong, thus demonstrating that both theoretically and practically the behavior contains information, not random noise. – Eric Postpischil Aug 20 '19 at 11:30
  • I'm working on existing project, where 63 macros are already taken the next comes 64(which are my changes) for which i have to do left shift operation. – Sushil Kumar Aug 20 '19 at 11:58
  • 1
    @SushilKumar this information needs to go into your **question**. All of the answers are wrong then. – Antti Haapala -- Слава Україні Aug 20 '19 at 11:58
  • @SushilKumar "I'm working on existing project, where 63 macros are already taken the next comes 64". I still don't get it. You want to left shift 64 times a 64 bits variable ? – Guillaume Petitjean Aug 20 '19 at 13:06
  • @EricPostpischil "This does not answer the question". I don't agree. I does answer the question (result is undefined behaviour) and then it makes sense to clarify why Sushil needs to do that. There might be some misunderstanding somewhere. – Guillaume Petitjean Aug 20 '19 at 13:09
  • @GuillaumePetitjean: That the result is undefined behavior does not answer the question. The question is not “What happens when you shift by more than the left operand width?” but “How do I shift by more than 64 bits?” Correct answers to that are to use a left operand wider than 64 bits or to construct the operation out of a compound type, as with a structure or array containing multiple elements. Saying the behavior of left shifting an operand not wider than 64 bits by 64 bits does not tell you how to shift by 64 bits, so it is not an answer. – Eric Postpischil Aug 20 '19 at 13:25
  • @GuillaumePetitjean: Framing this in a way that may make it clearer, the question solicits information about how to shift by more than 64 bits. That is a possible act, and there are ways to do it, but the answer that a left-shift of a 64-bit operand by more than 64 bits has undefined behavior does not provide that information. Since it does not provide the information that was solicited, it does not answer the question. It is really quite simple: If a person says “Please give me X,” and you say “You cannot have X in some particular way, here is Y,” then you have not done what was asked. – Eric Postpischil Aug 20 '19 at 13:35
  • 1
    Would have been much faster for you (and me by the way) to answer the question that nitpicking about the nature an answer should have... I understand we should follow rules on SO but this doesn't prevent to be flexible and remain constructive... – Guillaume Petitjean Aug 20 '19 at 13:40