473,387 Members | 1,621 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,387 software developers and data experts.

Bits Per Byte

Is there any constant in the .NET API Framework indicating the # of
bits per byte (or int, or anything else)? Kinda like
std::numeric_limits for C#? I know they're all constant (unlike C/C++)
I just have a built-in aversion to hardcoding 8 and 32 all over the
place in my code.

Thanks,
-ken
Nov 15 '05 #1
3 5693
Put something like the following in your Global library and make em public
static or what ever you like:
You could also use sizeof(), but you need to use unsafe context for that, so
marshal works for this without needing unsafe.

int bitsInByte = Marshal.SizeOf(typeof(byte)) * 8;
int bitsInInt = Marshal.SizeOf(typeof(int)) * 8;
int bitsInLong = Marshal.SizeOf(typeof(long)) * 8;
Console.WriteLine("Bits in byte:"+bitsInByte);
Console.WriteLine("Bits in Int:"+bitsInInt);
Console.WriteLine("Bits in Long:"+bitsInLong);

--
William Stacey, MVP

"Ken Durden" <cr*************@hotmail.com> wrote in message
news:18**************************@posting.google.c om...
Is there any constant in the .NET API Framework indicating the # of
bits per byte (or int, or anything else)? Kinda like
std::numeric_limits for C#? I know they're all constant (unlike C/C++)
I just have a built-in aversion to hardcoding 8 and 32 all over the
place in my code.

Thanks,
-ken

Nov 15 '05 #2
You can use sizeof to find the number of bytes for value
types.

I don't know of any predefined constant for the number of
bits in a byte (doesn't mean there isn't one), but
this'll work spendidly when placed in your source code:

private const int bitsInByte = 8;

DB connection strings and user names are one thing, but
it's ok to hardcode the color of the sun (yellow) or what
humans breath (air) or, for that matter, how many bits in
a byte (8 - It's not going to change).

If any of those things change, you're app likely will
have long since gone the way of the dinosaur.
-----Original Message-----
Is there any constant in the .NET API Framework indicating the # ofbits per byte (or int, or anything else)? Kinda like
std::numeric_limits for C#? I know they're all constant (unlike C/C++)I just have a built-in aversion to hardcoding 8 and 32 all over theplace in my code.

Thanks,
-ken
.

Nov 15 '05 #3
I understand what you mean, but in both C# and the CLI, the byte datatype's
very definition is an 8-bit unsigned value. This is quite unlike the header
definitions in C/C++, which are allowed to change.
Considering the submission to standards, I doubt it will change. I have a
feeling it was part of the design to stop the tangle of type redefinition.
The only type I know that has a dynamic size in the framework is IntPtr,
which is defined to be platform-specific (in other words, 32-bit on 32-bit
systems, 64 bit on 64-bit systesm, etc.).

-Rob Teixeira [MVP]

"Ken Durden" <cr*************@hotmail.com> wrote in message
news:18**************************@posting.google.c om...
Is there any constant in the .NET API Framework indicating the # of
bits per byte (or int, or anything else)? Kinda like
std::numeric_limits for C#? I know they're all constant (unlike C/C++)
I just have a built-in aversion to hardcoding 8 and 32 all over the
place in my code.

Thanks,
-ken

Nov 15 '05 #4

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

Similar topics

3
by: Ken | last post by:
I need to convert 32 bit Windows bitmaps to jpgs with PHP. I used the function from http://groups.google.com/groups? hl=en&lr=&ie=UTF-8&oe=UTF-8&safe=off&selm=a164f4b5.0311302128.40fb37f4%...
10
by: Kristian Nybo | last post by:
Hi, I'm writing a simple image file exporter as part of a school project. To implement my image format of choice I need to work with big-endian bytes, where 'byte' of course means '8 bits', not...
11
by: Steve | last post by:
Hi, i know this is an old question (sorry) but its a different problem, i need to write a binary file as follows 00000011 00000000 00000000 00000101 00000000 11111111
2
by: James Dean | last post by:
Could anybody tell me the most efficient way to set the bits in a 1Bpp class. I am reading in byte lines of an image. The byte array tells me what what bits are turned on or off.....i do bit...
4
by: Lance | last post by:
I have an array of bytes that I need to convert into an array of Integers. But, the number of bits per value in the Byte array is not necessarily divisible by 8 (although it will never exceed...
5
by: Oyvind Eriksen | last post by:
Hello. I need to read bits from bytes in a file. I have code that works but it's very slow. Can anybody help me? The code I have is: private bool GetBit(byte b, int pos) { return ((b &...
23
by: Umesh | last post by:
This is a basic thing. Say A=0100 0001 in ASCII which deals with 256 characters(you know better than me!) But we deal with only four characters and 2 bits are enough to encode them. I want to...
77
by: borophyll | last post by:
As I read it, C99 states that a byte is an: "addressable unit of data storage large enough to hold any member of the basic character set of the execution environment" (3.6) and that a byte...
29
by: Virtual_X | last post by:
As in IEEE754 double consist of sign bit 11 bits for exponent 52 bits for fraction i write this code to print double parts as it explained in ieee754 i want to know if the code contain any...
11
by: JoeC | last post by:
I am working on a graphics program but my question has nothing to do with graphics but trying to get an algorithm to work. I set graphics from a 16x16 grid to bits of a graphic with: bitData =...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.