473,498 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

basic binary code help

1 New Member
Im trying to do this for work. I am to design a binary code for encoding words. They are both upper and lower case, basic punctuation,16 different font styles, bold, itilicied, underlined or regular, font sizes (10-30, even numbers)

i need the least number of bits used to represent each aspect of the given scenario

i need to specify codes for representing each aspect and to code a sentence.

Also, specify the coding scheme so anyone can follow it to uniquely decode a read the word/sentence

Finally, I need to give an example of a coded sentence and explain howu your codes so that the sentence can be interpreted

Thanks for any help
Nov 14 '06 #1
1 2243
macklin01
145 New Member
Im trying to do this for work. I am to design a binary code for encoding words. They are both upper and lower case, basic punctuation,16 different font styles, bold, itilicied, underlined or regular, font sizes (10-30, even numbers)

i need the least number of bits used to represent each aspect of the given scenario

i need to specify codes for representing each aspect and to code a sentence.

Also, specify the coding scheme so anyone can follow it to uniquely decode a read the word/sentence

Finally, I need to give an example of a coded sentence and explain howu your codes so that the sentence can be interpreted

Thanks for any help
Well, let's see here ....

There are 2^4 = 16 font styles, so 4 bits there.

There are 2^4 < 21 < 2^5 font sizes, so 5 bits there but with some waste.

bold, italicized, underlined or regular: 4 = 2^2, so 2 bits with no waste.

2^4 < 26 < 2^5 letters, so 5 bits there with room for 7 punctuation symbols. I guess tab, space, and newline might be considered 3 of these punctuation symbols.

lowercase or uppercase: 1 bit.

Total: 17 bits, with a slight amount of waste on the font sizes. This is just over 2 bytes, so you'd need 3 bytes. Interestingly enough, that's (red,green,blue) in true color. :-)

You should probably make some sort of struct that is 3 bytes long. IIRC, however, you can't guarantee the memory alignment in a struct that isn't a multiple of 4 bytes, so you may need to throw yet another byte in there for padding. You can access individual bits with bit shift and boolean operations.

One thing to note: This assumes that every single character that you encode for can be a different font size, style, type, etc. If font size, style, upper/lowercase, etc. are "state variables" (current font size, style, shape, etc.), you could condense this somewhat. One bit can encode to say either formatting or data.

Then, you have about 12 bits for style, and 5 bits for characters. The great thing about that is that it fits inside a 2 byte struct!

bit 0: format or character

if format mode:
bit 1: caps or lowercase
bits 2-3: bold, italics, underline, regular
bits 4-7: font style
bits 8-12: font size
bits 13-15: unused. might as well allow more font sizes

if charcter mode:
bits 0-7: extended characters (yen symbol, etc.)
bits 8-15: one regular byte, so just take the unsigned character and put it here

Super efficient, with more options than called for. However, it might be possible to make it even more efficient. Notice how we got it under two bytes by allowing formating and character modes. Now, consider 4 modes, instead of two. that takes up 2 bits. Is it possible to squeeze this into one byte?

Notice that there are three types of fields that are relatively large: font size, font style, and what letter to encode. The largest of these is 5 bits. That's 3 categories.

Hmm ...

category 0: font style
category 1: font size
category 2: b/i/u/r and upper/lower
category 3: character encoding

bits 0-1: choose a category
bits 2-7: you get 6 bits to encode the stuff, and only need at most 5.

Yep. This fits the whole thing in 1 byte. So, imagine this:

byte 1: category 0 - set the current font style
byte 2: category 1 - set the current font size
byte 3: category 2 - set the current b/i/u/r and upper/lower
byte 4: category 3 - encode the first letter, probably capitalized.
byte 5: category 2 - set current b/i/u/r and upper/lower
byte 6+: category 3 - encode the remaining letters.

See what I mean? -- Paul
Nov 15 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

19
3175
by: Leif K-Brooks | last post by:
Has anyone ever tried implementing a simple unstructured BASIC dialect in Python? I'm getting interested in language implementation, and looking at a reasonably simple example like that could be...
27
4881
by: Eric | last post by:
Assume that disk space is not an issue (the files will be small < 5k in general for the purpose of storing preferences) Assume that transportation to another OS may never occur. Are there...
1
3380
by: Jerry Khoo | last post by:
hello, everybody, i am kinda new here, nice to meet u all. Now, i am > cs students and are now facing difficult problems in understanding > what a binary tree is, how it works, and the algorithm...
5
2174
by: nickisme | last post by:
Hi - sorry for the possibly stupid question, but I'm still a wee starter on c++... Just wondering if there's a quick way to convert data into binary strings... To explain, I'm trying to convert...
15
5064
by: Foodbank | last post by:
Hi all, I'm trying to do a binary search and collect some stats from a text file in order to compare the processing times of this program (binary searching) versus an old program using linked...
2
1496
by: craig.burrell | last post by:
I'm not a Javascript programmer, and I have a basic question about how scripts may make use of libraries in Javascript. I thank everyone for humouring me. Do all of the libraries required by a...
1
3302
kirubagari
by: kirubagari | last post by:
I Would Like Ask Ques On How To Open Bonding.bin File In C++ Or In Visual Basic 6.actually The Data In The Binary File Have Been Corrupted And I Would Like To Know The Method How To Open The File...
20
2366
by: DemonFox | last post by:
i have started my midterm exersize than is on binary treescan anyone help me on the basics i have started and i have made the following on my tree.h file: struct treenode { int data;...
6
38451
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7002
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...
0
7165
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,...
1
6885
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...
0
7379
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...
0
5462
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3081
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1417
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 ...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.