473,405 Members | 2,262 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,405 software developers and data experts.

Effect of integer type in bitwise operations performance

152 100+
hi!

when using the bitwise operations such as in:

Expand|Select|Wrap|Line Numbers
  1. int a = ...;
  2. int b = ... ;
  3.  
  4. a & b;
  5. a | b;
  6. ...
does it matter which type we use for performance?? (int vs long vs ..)

thanks!
Jan 9 '12 #1
3 2491
weaknessforcats
9,208 Expert Mod 8TB
Yeah it does. You do not want to use any signed integers like int short or long. Be sure these are all unsigned int, unsigned long, unsigned short.

The reason is that signed variables can be negative and that means the values could be stored in 2's complement.
Jan 10 '12 #2
donbock
2,426 Expert 2GB
The relative performance of bitwise operators on unsigned int versus unsigned long will typically be different for each compiler implementation.

An implementation is free to choose any size for unsigned int between that of unsigned short and unsigned long. (Thus, for some implementations there is no practical difference between unsigned int and unsigned long.) Typically, an implementation will use this freedom to select the best size for int; however, there are many dimensions along which to measure relative performance so this choice is necessarily a compromise. I would expect int to in general have the same or higher performance than long, but it could go the other way for any particular operator.

You could measure the relative performance for your implementation and choose accordingly. However, you need to understand that the results could be quite different when you port your program to a different implementation.
Jan 10 '12 #3
Banfa
9,065 Expert Mod 8TB
If you want to optimise for speed and you are using the C99 standard then you could always use one of the types, defined in stdint.h
Expand|Select|Wrap|Line Numbers
  1. // unsigned
  2. uint_fast8_t
  3. uint_fast16_t
  4. uint_fast32_t
  5. uint_fast64_t
  6.  
  7. //signed
  8. int_fast8_t
  9. int_fast16_t
  10. int_fast32_t
  11. int_fast64_t
  12.  
These are defined by the standard as

fastest unsigned unsigned integer type with width of at least 8, 16, 32 and 64 bits respectively
That is a uint_fast8_t might well have 32 bits on a platform that has best performance for integers with 32 bits.
Jan 11 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Fabrício de Novaes Kucinskis | last post by:
Hi all, I'm doing some bitwise operations in VB.NET, and I got some strange results. If an Int16 (Short) variable has the value &HAA (8 bits) and I do a 4-bit shift: var16 = var16 << 4 I...
2
by: Joe Gonzalez | last post by:
Is it possible to perform bitwise operations in selects in DB2 8.1? In postgres I can say something like SELECT ... FROM <sometable> WHERE RecFlags && 0x08 <> 0 to get a bitwise and operation. I...
9
by: Christopher Weaver | last post by:
I know that the bitwise AND of 8 and 4 will return 0 or false and the bitwise AND of 8 and 9 will return 1 or true but I don't know how to write the synax for it in C#. I have a value that ranges...
10
by: Emilio | last post by:
Do I use 'or' for bitwise operations where in c# I use | ?
4
by: AMDRIT | last post by:
Gang, I always get confused when it comes to 1's and 0's. I would like to perform a bitwise operation on a value based on checked boxes. Am I doing this right? assuming...
3
by: bill | last post by:
All, I have not visited Access for a while, and I am drawing a blank on how to search/sort a column of data (integer) for/on a particular bit pattern. Actually, a SQL example would be great. ...
4
by: siryuhan | last post by:
I am trying to apply bitwise operations on float, double, and long double values. I do not believe this is possible natively, so I created a wrapper class to construct a double value given two...
10
by: Rob Wilkerson | last post by:
I'm attempting to do some work around existing code that uses bitwise operations to manage flags passed into a function and I'm quite frankly unequipped to do so. I've never done much with bitwise...
8
by: Daniel Gutson | last post by:
Hi, I just wanted to share another library for doing type-safe bitwise operations in C++: http://bitwise-enum.googlecode.com I found it useful, so hopefully it'll be for somebody else as well....
1
by: DDAVE2010 | last post by:
I currently have the display method down, but I am not sure where to go with the main method... Write a program that uses bitwise operations to: (1) generate and display all power-of-two number in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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,...
0
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...
0
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,...
0
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...
0
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...
0
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...

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.