in All availableThis forumThis topic
Author Message
LoTeK
Apprentice

Joined: 26 Jul 2012
Posts: 270

 Posted: Fri Apr 19, 2013 10:00 am    Post subject: type definition in C: howto define a binary type? does anyone know how to define a type that has the size of one bit? I have to write some testbranches for an ALU (verilog) and I want to be able to compute with a type that is exactly like it's real implementation. So it must be a 32bit-array and the elements must have the size of 1bit. I know of course that I could use bool, but I would like to know how to implement such a type by myself. Since it is very easy to create new types with existing one and because one can access bits in C, this should be possible?! (has the type bool in C really the size of 1 bit?)_________________"I want to see gamma rays! I want to hear X-rays! Do you see the absurdity of what I am? I can't even express these things properly because I have to conceptualize complex ideas in this stupid limiting spoken language!"
Prenj
n00b

Joined: 20 Nov 2011
Posts: 10
Location: Mostar, BiH

Posted: Fri Apr 19, 2013 10:09 am    Post subject: Re: type definition in C: howto define a binary type?

 LoTeK wrote: does anyone know how to define a type that has the size of one bit? I have to write some testbranches for an ALU (verilog) and I want to be able to compute with a type that is exactly like it's real implementation. So it must be a 32bit-array and the elements must have the size of 1bit. I know of course that I could use bool, but I would like to know how to implement such a type by myself. Since it is very easy to create new types with existing one and because one can access bits in C, this should be possible?! (has the type bool in C really the size of 1 bit?)

what's the size of bool: http://stackoverflow.com/questions/1608318/is-bool-a-native-c-type

how to shift bits n stuff: http://www.edaboard.com/thread214799.html

Not sure if there is a 1-bit type in C99, but back in the days, bool didn't exist and it was macroed on top of int
LoTeK
Apprentice

Joined: 26 Jul 2012
Posts: 270

Posted: Fri Apr 19, 2013 10:19 am    Post subject:

 Quote: Not sure if there is a 1-bit type in C99, but back in the days, bool didn't exist and it was macroed on top of int

yes I know, but since int has a size of 32bits in most systems it's a bit a waste to use such a type to model true and false. Even the type char would be better.

http://www.cs.cf.ac.uk/Dave/C/node13.html
this was a link at the bottom of your link. seems a bit strange to define a struct with several unsigned int members just to define a binary type.

I thought back in the early days of C memory was expensive and they tried to write code as memory efficient as possible, so it's strange that there isn't a bit type and the lack of booleans.

I wonder how they program things like those mars-robots. don't they have to spare every possible bit?
_________________
"I want to see gamma rays! I want to hear X-rays! Do you see the absurdity of what I am? I can't even express these things properly because I have to conceptualize complex ideas in this stupid limiting spoken language!"
alistair
Retired Dev

Joined: 15 Jul 2005
Posts: 869

 Posted: Fri Apr 19, 2013 10:57 am    Post subject: I don't know a whole lot about cpu architectures but I would probably say those assertions about memory efficiency are wrong Lotek. If you think about it x32 and 32 bit registers. whether you stick 1 bit, byte, or int into it the 'memory' consumption of the cpu remains the same (aka one register). If we start thinking about L1/L2 caches, I would expect that they are all optimized for moving large ( compared to a single bit ) amounts of memory around, I would assume it would be difficult and inefficient to attempt to support single bits. Also note that if the cache moves a power of 2 around, then supporting 1 bit would mean that essentually you lose a whole lot of bits as waste ( e.g. if its 32 bit and you transfer a single bit, what are the 31 other bits doing)._______________________________ Help the gentoo-java project. Visit Gentoo Java Project what good are admin powers if you don't abuse them for personal gain - mark_alec
LoTeK
Apprentice

Joined: 26 Jul 2012
Posts: 270

 Posted: Fri Apr 19, 2013 11:48 am    Post subject: I see, but I would not want to move around single bits, but if the CPU has 32bit registers I would want to work with 32bit arrays and interprete them as 32digit binary numbers. So an int is actually a 32bit-array, isn't it? I realize I could work with them after all. but if you are right why do people use for example short int? Since they have a size of 16bits on most systems, why use them on a 32bit architecture?_________________"I want to see gamma rays! I want to hear X-rays! Do you see the absurdity of what I am? I can't even express these things properly because I have to conceptualize complex ideas in this stupid limiting spoken language!"
Prenj
n00b

Joined: 20 Nov 2011
Posts: 10
Location: Mostar, BiH

Posted: Fri Apr 19, 2013 1:02 pm    Post subject:

 LoTeK wrote: I see, but I would not want to move around single bits, but if the CPU has 32bit registers I would want to work with 32bit arrays and interprete them as 32digit binary numbers. So an int is actually a 32bit-array, isn't it? I realize I could work with them after all. but if you are right why do people use for example short int? Since they have a size of 16bits on most systems, why use them on a 32bit architecture?

int is not guaranteed 32-bit, its architecture dependant.

do a sizeof(int)
LoTeK
Apprentice

Joined: 26 Jul 2012
Posts: 270

Posted: Fri Apr 19, 2013 1:20 pm    Post subject:

 Quote: int is not guaranteed 32-bit, its architecture dependant. do a sizeof(int)

I know, but it's 32bit on most systems.
_________________
"I want to see gamma rays! I want to hear X-rays! Do you see the absurdity of what I am? I can't even express these things properly because I have to conceptualize complex ideas in this stupid limiting spoken language!"
Prenj
n00b

Joined: 20 Nov 2011
Posts: 10
Location: Mostar, BiH

Posted: Fri Apr 19, 2013 2:01 pm    Post subject:

LoTeK wrote:
 Quote: int is not guaranteed 32-bit, its architecture dependant. do a sizeof(int)

I know, but it's 32bit on most systems.

hence loads of money being burned fixing stuff like that.
couple of things you cannot take for granted:

- sizeof(something)
- byte order (ok less common buy still)
- alignment
LoTeK
Apprentice

Joined: 26 Jul 2012
Posts: 270

Posted: Fri Apr 19, 2013 2:52 pm    Post subject:

Prenj wrote:
LoTeK wrote:
 Quote: int is not guaranteed 32-bit, its architecture dependant. do a sizeof(int)

I know, but it's 32bit on most systems.

hence loads of money being burned fixing stuff like that.
couple of things you cannot take for granted:

- sizeof(something)
- byte order (ok less common buy still)
- alignment

indeed. I always write for example
 Code: int *foo = malloc (n * sizeof (int));
even if I know that I will run the program only on one machine. Maybe the byte order is not very common because most systems are little endian today? What do you mean with alignment problems?

but are they any desktops/laptops that don't have 32bit integer size today? I thought smaller sizes are common in embedded systems, smartphones etc.
_________________
"I want to see gamma rays! I want to hear X-rays! Do you see the absurdity of what I am? I can't even express these things properly because I have to conceptualize complex ideas in this stupid limiting spoken language!"
Prenj
n00b

Joined: 20 Nov 2011
Posts: 10
Location: Mostar, BiH

Posted: Fri Apr 19, 2013 3:01 pm    Post subject:

 LoTeK wrote: What do you mean with alignment problems?

Not sure if you're gonna encounter this one nowadays, but back then it used to make your head explode:

pigeon768
l33t

Joined: 02 Jan 2006
Posts: 669

 Posted: Fri Apr 19, 2013 7:09 pm    Post subject: You can't make an independent data structure that takes just 1 bit, or indeed anything other than a multiple of 8. (some architectures require a multiple of 4, 8, or even 16 bytes) You can use a bit field with 32 (or whatever) 1 bit objects in it. Then you access them with 'alu_data.property_1' or whatever._________________My political bias.
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First
 All times are GMT Page 1 of 1

 Jump to: Select a forum Assistance----------------News & AnnouncementsFrequently Asked QuestionsInstalling GentooMultimediaDesktop EnvironmentsNetworking & SecurityKernel & HardwarePortage & ProgrammingGamers & PlayersOther Things GentooUnsupported Software Discussion & Documentation----------------Documentation, Tips & TricksGentoo ChatGentoo Forums FeedbackOff the WallDuplicate Threads International Gentoo Users----------------中文 (Chinese)DutchFinnishFrenchDeutsches Forum (German)  Diskussionsforum  Deutsche DokumentationGreekForum italiano (Italian)  Forum di discussione italiano  Risorse italiane (documentazione e tools)Polskie forum (Polish)  Instalacja i sprzęt  Polish OTWPortuguese  Documentação, Ferramentas e DicasRussianScandinavianSpanishOther Languages Architectures & Platforms----------------Gentoo on AMD64Gentoo on PPCGentoo on SparcGentoo on Alternative ArchitecturesGentoo for Mac OS X (Portage for Mac OS X)
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum