Skip to main content
GameDev.net gamedev.net
🔒 Locked

BITS Question

Started by Don GL Jan 6, 2007 at 11:00 AM 6 replies 550+ views
Original Post
Don GL
Don GL
Hello, I've got a question on bits. kinda basic... I have a DWORD to set some char options in an edit box; e.g. DWORD format = CFM_BOLD|CFM_COLOR|CFM_ITALIC|CFM_UNDERLINE; but I would like to string it up according to some parameters I have a function setformat ( bool bold, bool italic, bool underline ) so I would like to set the format string according to the parameters if ( bold ) format += CFM_BOLD if ( italic) format += CFM_ITALIC etc now, += doesn't work.... how can this be done... I've tried bit operators like & and | etc, but I know only how to read stuff with them, not how to set stuff. thanks in advance
cignox1
cignox1
AFAIK the | operator should do the job.
i.e:
if(bold) format |= CFM_BOLD;

This only works if flags are powers of two...
But I'm very tired right now, so I can be wrong :-(

EDIT: just an example:

DWORD CFM_BOLD = 1;DWORD CFM_COLOR = 2;DWORD CFM_ITALIC = 3;DWORD format = CFM_BOLD; //0...001/*Bit-wise or is used on format to add a flag. Thuis means that:0...001 |0...010 =0...011*/format |= CFM_COLOR;/*If you want to get the status of a flag then use bit-wise and:0...011 &0...010 =0...010*/DWORD color = format & CFM_COLOR;

Should work, I hope...
Zakwayda
Zakwayda
You're looking for 'bitwise OR', i.e.:
if ( bold ) format |= CFM_BOLD;if ( italic) format |= CFM_ITALIC;
Don GL
Don GL
thanx, it works :)
however, the += seems to work also, but only if I initialize the DWORD to 0
is this true or simply luck?
Scet
Scet
OR only sets bits where as add will actually do addition.

Example:

DWORD = 4 ( 0100 )
DWORD |= 4;
DWORD is still 4;

DWORD = 4 ( 0100 )
DWORD += 4;
DWORD is now 8 ( 1000 ) so the flags are wrong;

Quote:
Original post by cignox1
But I'm very tired right now, so I can be wrong :-(


CFM_ITALIC should be 4, not 3. If it's 3 then it will also set bold and color.
TheUnbeliever
TheUnbeliever
Quote:
Original post by Don GL
thanx, it works :)
however, the += seems to work also, but only if I initialize the DWORD to 0
is this true or simply luck?


This works only if you do not add two flags with the same value. You should not rely on it, and should stick to bitwise OR. You can see why:

Example
Setting two flags: one with a value of 1, one with a value of 2.

0000 0000 + 1 =
0000 0001 + 2 =
0000 0011

0000 0000 OR 1 =
0000 0001 OR 2 =
0000 0011

However, try and change the same bit twice and it goes to pot (i.e. this only works for flags based on powers of two - or, therefore, which only set 1 bit).

Example
Setting two flags: one with a value of 1, one with a value of 3 (because this second case requires two flags to be set)

0000 0000 + 1 =
0000 0001 + 3 =
0000 0100

0000 0000 OR 1 =
0000 0001 OR 3 =
0000 0011

This is also why you need to initialise it to 0, obviously.

General bitmasking tips:
To retrieve the value of a certain bit, use bitwise AND (&)
To set the value of a certain bit, use bitwise OR (|)
To toggle the value of a certain bit, use bitwise XOR (^)
[TheUnbeliever]
Don GL
Don GL
thanx very much!
problem is more than solved :)
cignox1
cignox1
Quote:

CFM_ITALIC should be 4, not 3. If it's 3 then it will also set bold and color.

Yeah, and I even said they must be powers of two... new rule for myself: never post when tired :-)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.