473,662 Members | 2,524 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

typecasting a bitfield to unsigned long long

hi Gurus

I'm a newbie learning C.
I've a question, relevant code snippet is as follows :

typedef unsigned long long ulonglong;

struct foo {
unsigned dummy : 2;
unsigned n : 24;
unsigned c : 3;
unsigned d : 1;
unsigned v : 1;
unsigned g : 1;
};

ulonglong func_foo(void) {
ulonglong addr;
struct foo entry;

/* code that populates entry snipped*/

addr = entry.n << 12;
..
..
}

This caused sign extended value in addr which is not we want.
Would the following be a good fix or clc gurus have a better suggestion?

addr = ((ulonglong)ent ry.n) << 12;

I'm concerned that there is more to it than what I feel the fix is.

Thanks in advance
- Sushil
Nov 13 '05 #1
1 2994
On 27 Nov 2003 20:34:04 -0800, in*******@yahoo .com (Sushil) wrote in
comp.lang.c:
hi Gurus

I'm a newbie learning C.
I've a question, relevant code snippet is as follows :

typedef unsigned long long ulonglong;

struct foo {
unsigned dummy : 2;
unsigned n : 24;
unsigned c : 3;
unsigned d : 1;
unsigned v : 1;
unsigned g : 1;
};

ulonglong func_foo(void) {
ulonglong addr;
struct foo entry;

/* code that populates entry snipped*/

addr = entry.n << 12;
This is a problem, due to C's value preserving promotion rules.
Assuming ints have more than 24 bits on your platform, the unsigned
value in entry.n is converted to a signed int, then left shifted 12
times producing a signed int value. If your ints have 32 bits, the
top four bits of entry.n are shifted off the end and disappear.
.
.
}

This caused sign extended value in addr which is not we want.
Would the following be a good fix or clc gurus have a better suggestion?

addr = ((ulonglong)ent ry.n) << 12;
This will work just fine. The 24 bits in entry.n will be placed in
the lowest 24 bits of an unsigned long long. No sign extension will
occur, and there is no possibility of overflow.

I'm concerned that there is more to it than what I feel the fix is.

Thanks in advance
- Sushil


There are ways to define a union consisting of a bit-mapped structure
and an unsigned long long, but doing it with the cast is more portable
and doesn't need to be modified if any of the bit-fields change size,
position, or are added or deleted.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #2

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

Similar topics

9
5250
by: Davide Bruzzone | last post by:
Greetings all... I need to create a number of bitfield structs whose contents are smaller than the size of an int. For example: typedef struct foo FOO; struct foo { unsigned char fieldOne: 2, fieldTwo: 6; };
4
10460
by: Ray | last post by:
When a single-bit bitfield that was formed from an enum is promoted/cast into an integer, does ANSI C say anything about whether that integer should be signed or unsigned? SGI IRIX cc thinks it is an unsigned integer, so I see a +1 if the bit is set. Microsoft VC++ thinks it's signed, so I see -1 if the bit is set. Ex. typedef enum {
1
2825
by: b83503104 | last post by:
When are they not consistent?
63
3348
by: andynaik | last post by:
Hi, Whenever we type in this code int main() { printf("%f",10); } we get an error. We can remove that by using #pragma directive t direct that to the 8087. Even after that the output is 0.00000 and no 10.0000. Can anybody tell me why it is like that and why typecasting i not done in this case?
26
12546
by: Nishu | last post by:
Hi All, Is it valid in C to typecast a pointer? eg. code snippet... considering int as 16 bit and long as 32 bit. int *variable, value; *((long*)variable)++ = value; *((long*)variable)++ = value;
3
1521
by: nass | last post by:
hello everyone, i read somewhere that it is possible to define a BitFields structure so i set out to: struct IC_X { unsigned short int dump:2; unsigned short int ic1:10; unsigned short int ic2:10; unsigned short int ic3:10;
7
7119
by: arne | last post by:
Hi all, cleaning up some elderly code, I stumbled across the following: /**************************************************/ struct { uint bf:8; char a1; char a2;
6
2576
by: shaun roe | last post by:
For a bit of seasonal festive fun, I thought I'd try making a bitfield function, i.e. a function returning, for example, the value of bits 1 to 5 of a word as an integer, when the exact bits are known at compile time. The data word is always 32 bit unsigned integer (its a data stream from some embedded controller). The legacy code I have has lines like: errNum = (dataWord>>1) & 31; //bits 1 to 5 is an error code
4
825
by: mail.dsp | last post by:
Consider following: unsigned char i= 0; Now when we perform "++i" Is "i" typecasted into "int", "++" operator increment its value and finally typecasted into "unsigned char"? Thanks in advance -- Daya
0
8344
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8857
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...
1
8546
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
8633
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
6186
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
4180
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...
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1993
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.