skellt

skellt

Apple Game Frameworks and Technologies: Wrote binary, meant decimal (p 85)

At the bottom of page 85 is this info box:

By default, integer literals are expressed in decimal using the numbers 0 through 9. However, you can modify this default behavior and use binary literals instead, which might make it easier. With binary literals, you use the prefix 0b. Unlike integer literals, binary literals use only 0 and 1. So, in this example, 0b100 equals the number four. Essentially, this is shorthand for 000000100. If you choose to use binary literals, you’ll need to work in squares: 1, 2, 4, 8, 16, and so on.

The last sentence says If you choose to use binary literals, you’ll need to work in squares: 1, 2, 4, 8, 16, and so on. I think that’s a typo and was meant to read: “If you choose to use decimal literals…”

Marked As Solved

Paradox927

Paradox927

Author and Editor at PragProg

Hello, David.

An integer literal represents some arbitrary value. You can express this value as a decimal, binary, octal, or hexadecimal constant. Binary literals begin with 0b, octal literals begin with 0o, and hexadecimal literals begin with 0x. Decimal literals have no prefix—they are what they are.

In this case, the physics category is a single 32-bit integer that acts as a mask, and each bit represents a single category, which is why you’re limited to 32 categories per scene. With masking, using powers of two makes things easier—especially when using binary literals.

Now, having said that, what you pointed out wasn’t actually a typo, but the information was certainly misleading. What it should have read–and now does read–is as follows:

When setting your categories, it’s best to do so using powers of two: 1, 2, 4, 8, 16, and so on.

You’ll see this new explanation in the next update.

Also, later in the book, I explain how to create and use a Hashable OptionSet to set up and retrieve the physics category—something that is also mentioned in that same aside. Enums are great, and for simpler games, preferred. However, as your game grows in complexity, Hashable OptionSets provide a bit more flexibility. You’ll see this solution in use when you get to Val’s Revenge.

Thanks again for pointing this out. Have a great day.

Also Liked

skellt

skellt

When setting your categories, it’s best to do so using powers of two: 1, 2, 4, 8, 16, and so on.

Yes I agree this will read better. Regardless of which system we use, the mask values ought to be 1, 2, 4, 8, 16 etc… In decimal we will actually use the literals 1, 2, 4, 8, 16.

In binary we will be representing those values using only 0, 1, and the letter b (e.g. 0b100 for 4). It was confusing previously when it said that if you choose to use binary you’ll need to work with 1, 2, 4. You were referring to the mask values, I thought you were referring to the literals. The new text makes it clear you are referring to the category values.

Where Next?

Popular Pragmatic Bookshelf topics Top

telemachus
Python Testing With Pytest - Chapter 2, warnings for “unregistered custom marks” While running the smoke tests in Chapter 2, I get these...
New
brianokken
Many tasks_proj/tests directories exist in chapters 2, 3, 5 that have tests that use the custom markers smoke and get, which are not decl...
New
GilWright
Working through the steps (checking that the Info,plist matches exactly), run the demo game and what appears is grey but does not fill th...
New
New
Charles
In general, the book isn’t yet updated for Phoenix version 1.6. On page 18 of the book, the authors indicate that an auto generated of ro...
New
oaklandgit
Hi, I completed chapter 6 but am getting the following error when running: thread 'main' panicked at 'Failed to load texture: IoError(O...
New
adamwoolhether
Is there any place where we can discuss the solutions to some of the exercises? I can figure most of them out, but am having trouble with...
New
rainforest
Hi, I’ve got a question about the implementation of PubSub when using a Phoenix.Socket.Transport behaviour rather than channels. Before ...
New
a.zampa
@mfazio23 I’m following the indications of the book and arriver ad chapter 10, but the app cannot be compiled due to an error in the Bas...
New
redconfetti
Docker-Machine became part of the Docker Toolbox, which was deprecated in 2020, long after Docker Desktop supported Docker Engine nativel...
New

Other popular topics Top

Exadra37
I am thinking in building or buy a desktop computer for programing, both professionally and on my free time, and my choice of OS is Linux...
New
AstonJ
We have a thread about the keyboards we have, but what about nice keyboards we come across that we want? If you have seen any that look n...
New
AstonJ
I’ve been hearing quite a lot of comments relating to the sound of a keyboard, with one of the most desirable of these called ‘thock’, he...
New
New
Margaret
Hello content creators! Happy new year. What tech topics do you think will be the focus of 2021? My vote for one topic is ethics in tech...
New
Rainer
Not sure if following fits exactly this thread, or if we should have a hobby thread… For many years I’m designing and building model air...
New
AstonJ
Continuing the discussion from Thinking about learning Crystal, let’s discuss - I was wondering which languages don’t GC - maybe we can c...
New
AstonJ
We’ve talked about his book briefly here but it is quickly becoming obsolete - so he’s decided to create a series of 7 podcasts, the firs...
New
First poster: bot
The overengineered Solution to my Pigeon Problem. TL;DR: I built a wifi-equipped water gun to shoot the pigeons on my balcony, controlle...
New
AstonJ
If you want a quick and easy way to block any website on your Mac using Little Snitch simply… File > New Rule: And select Deny, O...
New

Sub Categories: