473,387 Members | 1,541 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.

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)entry.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 2975
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)entry.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.learn.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
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:...
4
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...
1
by: b83503104 | last post by:
When are they not consistent?
63
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...
26
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)++...
3
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...
7
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
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...
4
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...
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
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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.