473,382 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

bit shift and data type of a constant value

Hi,

when I use something like

int Shift= 3;
long Value= 1 << Shift;

What is the data type of the const value '1' here? In other terms: What
is the possible maximum of 'Shift' here?

If I want to make sure to make the shift operation work on at least
32 bit (given a 32 bit system) does it help to cast or is it senseless:

long Value=((unsigned int)1) << Shift;

Felix
Apr 4 '07 #1
4 4157
Felix Kater a écrit :
1)
What is the data type of the const value '1' here?

This is an integer constant since it is not suffixed with L, or LL. Type
is 'int'.

2)
What
What is the possible maximum of 'Shift' here?

It is sizeof(int)*CHAR_BIT - 1

3)
If I want to make sure to make the shift operation work on at least
32 bit (given a 32 bit system) does it help to cast or is it senseless:

long Value=((unsigned int)1) << Shift;
The cast is useless since '1' is ALREADY an int. But since you are
assigning to a long it would be more logical to write:

long Value = 1L << Shift;

In this case 1L is a long integer, and in 32 bit systems it will
be probably 32 bits, and in some 64 bit systems it will be 64 bits.

In this case,

long Value= 1L << Shift;

Shift <= sizeof(long)*CHAR_BIT - 1

In most systems, long long is 64 bits, so if you want a fairly portable
construct with maximum range use:

long long Value = 1LL << Shift;

then you will have Shift <= sizeof(long long)*CHAR_BIT - 1, in most
systems 63.

jacob
Apr 4 '07 #2
jacob navia wrote, On 04/04/07 08:34:

Jacob, what has happened to your quoting? Thunderbird normally inserts
the quote markers correctly, so I suspect you have accidentally changed
something. I've fixed the quoting.
Felix Kater a écrit :
>1)
What is the data type of the const value '1' here?

This is an integer constant since it is not suffixed with L, or LL. Type
is 'int'.
>2)
What
What is the possible maximum of 'Shift' here?

It is sizeof(int)*CHAR_BIT - 1
No, for signed types you need -2 to avoid overflow. For unsigned types
-1 is OK.
>3)
If I want to make sure to make the shift operation work on at least
32 bit (given a 32 bit system) does it help to cast or is it senseless:

long Value=((unsigned int)1) << Shift;

The cast is useless since '1' is ALREADY an int.
Incorrect, since the case converts it to *unsigned* int and thus allows
it to be shifted a bit further without any risk.
But since you are
assigning to a long it would be more logical to write:

long Value = 1L << Shift;
Yes.
In this case 1L is a long integer, and in 32 bit systems it will
be probably 32 bits, and in some 64 bit systems it will be 64 bits.

In this case,

long Value= 1L << Shift;

Shift <= sizeof(long)*CHAR_BIT - 1

In most systems, long long is 64 bits, so if you want a fairly portable
construct with maximum range use:

long long Value = 1LL << Shift;

then you will have Shift <= sizeof(long long)*CHAR_BIT - 1, in most
systems 63.
Not quite, if you want maximum range use an unsigned type, then there is
no risk of overflow.

In general it is best to use unsigned types for bit twiddling.
--
Flash Gordon
Apr 4 '07 #3
jacob navia wrote:
>
Felix Kater a écrit :
1)
What is the data type of the const value '1' here?

This is an integer constant since it is not suffixed with L, or LL. Type
is 'int'.

2)
What
What is the possible maximum of 'Shift' here?

It is sizeof(int)*CHAR_BIT - 1
That's too high.

Concerning (E1 << E2):
N869
6.5.7 Bitwise shift operators
[#4] The result of E1 << E2 is E1 left-shifted E2 bit
positions; vacated bits are filled with zeros. If E1 has an
unsigned type, the value of the result is E1×2E2, reduced
modulo one more than the maximum value representable in the
result type. If E1 has a signed type and nonnegative value,
and E1×2E2 is representable in the result type, then that is
the resulting value; otherwise, the behavior is undefined.
3)
If I want to make sure to make the shift operation work on at least
32 bit (given a 32 bit system)
does it help to cast or is it senseless:
>
long Value=((unsigned int)1) << Shift;

The cast is useless since '1' is ALREADY an int. But since you are
assigning to a long it would be more logical to write:
The cast to unsigned, prevents undefined behavior,
though (unsigned long) would be a better choice for the above.
Bit operations involving the sign bit,
are either implementation defined or undefined.
>
long Value = 1L << Shift;

In this case 1L is a long integer, and in 32 bit systems it will
be probably 32 bits, and in some 64 bit systems it will be 64 bits.

In this case,

long Value= 1L << Shift;

Shift <= sizeof(long)*CHAR_BIT - 1

In most systems, long long is 64 bits,
so if you want a fairly portable
construct with maximum range use:

long long Value = 1LL << Shift;

then you will have Shift <= sizeof(long long)*CHAR_BIT - 1, in most
systems 63.
long long Value = = 1LL << sizeof(long long)*CHAR_BIT - 1;

is undefined.

--
pete
Apr 4 '07 #4
Felix Kater wrote:
Hi,

when I use something like

int Shift= 3;
long Value= 1 << Shift;

What is the data type of the const value '1' here?
Because you've used no type suffixes it's an int value.
In other terms: What
is the possible maximum of 'Shift' here?
That'd be sizeof(1)*CHAR_BIT - 2.
If I want to make sure to make the shift operation work on at least
32 bit (given a 32 bit system) does it help to cast or is it senseless:

long Value=((unsigned int)1) << Shift;
int is only guaranteed to be atleast 16 bits. long is guaranteed to be
atleast 32 bits. So the cast above should be to long or unsigned long,
depending on the type of Value.

Apr 4 '07 #5

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

Similar topics

4
by: Glen Able | last post by:
Just to get my head straight on this... Firstly, am I right in thinking that right-shifting a signed integer has an undefined result (i.e. could be implemented as a logical or arithmetic shift)?...
9
by: GGG | last post by:
Noticed something odd in the way bit shifting was working today. As far as I have ever heard, shifting will shift in zeros(signed ints aside) However I foudn something odd when I am shifting...
388
by: maniac | last post by:
Hey guys, I'm new here, just a simple question. I'm learning to Program in C, and I was recommended a book called, "Mastering C Pointers", just asking if any of you have read it, and if it's...
4
by: Markus Hahn | last post by:
While coding some binary data handling I found this IMHO strange behavior: Dim bToShift As Byte = 1 bToShift <<= 9 Console.WriteLine("and the value is {0}", bToShift) The output is "2", I...
7
by: Csaba Gabor | last post by:
I'd like to detect the shift key when a button is "clicked" in Firefox/Mozilla. If the button is clicked with the mouse, no problem. However, if the onclick event is keyboard originated, then my...
56
by: Christian Christmann | last post by:
Hi, in the header of my class I've a constant static const int a = ( 1 << 32 ) - 1; When compiling the code, g++ issues the warning "warning: left shift count >= width of type" Why? And...
7
by: gokkog | last post by:
Hello, Recently I have the book Programming Pearls. Nice read! Perhaps it is weekend, I cannot understand the following codes well: #define BITSPERWORD 32 #define SHIFT 5 #define MASK 0x1F...
24
by: Nishu | last post by:
Hi All, Could you please explain whether C standard supports logical right shift operation using some operator? I know somewhere I read about >>operator. I thought, it is in C but i think i'm...
11
by: Bob Altman | last post by:
Hi all, I want to write a generic class that does this: Public Class X (Of T) Public Sub Method(param As T) dim x as T = param >3 End Sub End Class
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.