Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Bitfield and Union in C - strange results
#include <stdio.h> #include <string.h> struct Message { unsigned int hour : 5; unsigned int minutes : 6; unsigned int seconds : 6; unsigned int day : 5; unsigned int month : 4; unsigned int year : 12; unsigned long long int code : 26; }; union Msgdecode { long long int datablob; struct Message elems; }; int main(void) { long long int datablob = 131809282883593; union Msgdecode m; m.datablob = datablob; printf("%d:%d:%d %d.%d.%d code:%lu\n", m.elems.hour, m.elems.minutes, m.elems.seconds, m.elems.day, m.elems.month, m.elems.year,(long unsigned int) m.elems.code); union Msgdecode m2; m2.elems.hour = 9; m2.elems.minutes = 0; m2.elems.seconds = 0; m2.elems.day = 30; m2.elems.month = 5; m2.elems.year = 2017; m2.elems.code = 4195376; printf("m2.datablob: should: 131809282883593 is: %lld\n", m2.datablob); //TODO WHY does m2.datablob != m.datablob?! printf("m.datablob: should: 131809282883593 is: %lld\n", m.datablob); printf("%d:%d:%d %d.%d.%d code:%lu\n", m2.elems.hour, m2.elems.minutes, m2.elems.seconds, m2.elems.day, m2.elems.month, m2.elems.year, (long unsigned int) m2.elems.code); }
run
|
edit
|
history
|
help
0
A_141117_Euclides
Matrix rotation 90-degree
A141212_OrdenarArrayPorFunciones
Shapes and such
bitwise manipulation
b=1,c=1
Perfect number
loop
18BCE2182 ASSESS_3 Q2
Project 5 v.03