473,414 Members | 1,697 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Bitwise macro help

I have a macro which gets a bit from an unsigned char in a 2d array.
#define GET_PIX(x,y) (array[y][x / 8] >> (7 - x % 8)) & 1

I now need to work out a macro to set a pix to a value of 1 or 0
depending on what number I pass it. Any help would be very much
appreciated.

Nov 14 '05 #1
5 2030
Jo*********@gmail.com wrote:
I have a macro which gets a bit from an unsigned char in a 2d array.
#define GET_PIX(x,y) (array[y][x / 8] >> (7 - x % 8)) & 1 I now need to work out a macro to set a pix to a value of 1 or 0
depending on what number I pass it. Any help would be very much
appreciated.


I assume you mean that when the value is non non-zero you want to
set the pixel, otherwise reset it. So what about

#define SET_PIX(x,y,v) (array[y][x / 8] |= (v ? 1U : 0) << (7 - x % 8))

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #2
Je***********@physik.fu-berlin.de wrote:
Jo*********@gmail.com wrote:
I have a macro which gets a bit from an unsigned char in a 2d array.
#define GET_PIX(x,y) (array[y][x / 8] >> (7 - x % 8)) & 1

I now need to work out a macro to set a pix to a value of 1 or 0
depending on what number I pass it. Any help would be very much
appreciated.


I assume you mean that when the value is non non-zero you want to
set the pixel, otherwise reset it. So what about

#define SET_PIX(x,y,v) (array[y][x / 8] |= (v ? 1U : 0) << (7 - x % 8))
...


But for zero value of 'v' the right-hand side will evaluate to 0. What's
the point of doing '|=' with 0 as a rhs operand?

--
Best regards,
Andrey Tarasevich
Nov 14 '05 #3
Jo*********@gmail.com wrote:
I have a macro which gets a bit from an unsigned char in a 2d array.
#define GET_PIX(x,y) (array[y][x / 8] >> (7 - x % 8)) & 1

I now need to work out a macro to set a pix to a value of 1 or 0
depending on what number I pass it. Any help would be very much
appreciated.


One way to do it is

#define SET_PIX(x,y,v) ((v) ?\
array[y][(x) / 8] |= 1 << (7 - (x) % 8) :\
array[y][(x) / 8] &= ~(1 << (7 - (x) % 8)))

--
Best regards,
Andrey Tarasevich
Nov 14 '05 #4
Andrey Tarasevich <an**************@hotmail.com> wrote:
Je***********@physik.fu-berlin.de wrote:
Jo*********@gmail.com wrote:
I have a macro which gets a bit from an unsigned char in a 2d array.
#define GET_PIX(x,y) (array[y][x / 8] >> (7 - x % 8)) & 1

I now need to work out a macro to set a pix to a value of 1 or 0
depending on what number I pass it. Any help would be very much
appreciated.


I assume you mean that when the value is non non-zero you want to
set the pixel, otherwise reset it. So what about

#define SET_PIX(x,y,v) (array[y][x / 8] |= (v ? 1U : 0) << (7 - x % 8))
...

But for zero value of 'v' the right-hand side will evaluate to 0. What's
the point of doing '|=' with 0 as a rhs operand?


You're right, I forgot about unsetting the bit. So back to the
drawing board...

#define SET_PIX(x,y,v) \
do { if ( v ) \
array[y][x / 8] |= 1U << (7 - x % 8); \
else \
array[y][x / 8] &= ~ (1U << (7 - x % 8)); \
} while ( 0 )

I hope this makes more sense.
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #5
Jo*********@gmail.com wrote:
I have a macro which gets a bit from an unsigned char in a 2d array.
#define GET_PIX(x,y) (array[y][x / 8] >> (7 - x % 8)) & 1
You should 'protect' x and the whole expression with ( ) to avoid
problems, e.g. GET_PIX(1+2,y), if (GET_PIX(x,y) != 0) ...
I now need to work out a macro to set a pix to a value of 1 or 0
depending on what number I pass it.


This is all dealt with in the FAQ, but in what way is it
dependant? If b below is either 0 or 1...

#define SET_PIX(x,y,b) \
( array[y][(x)/8] |= (b) << (7 - (x) % 8 )

If x is required to be non-negative (likely), then...

#define SET_PIX(x,y,b) \
( array[y][(x) >> 3] |= (b) << (7 - ((x) & 7)) )

....may avoid compilation to innefficient code in the case
where x is a signed variable.

The behaviour of x % 8 and x & 7 is _not_ necessarily the
same if x is negative, but compilers are _required_ to
implement the correct semantics for the code supplied.

--
Peter

Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Michael B. Trausch | last post by:
I have a question regarding bitwise operators, I've been trying to figure this out for about two days now, and I just can't seem to get it. What I'm trying to do is use a variable to hold a bitmask...
2
by: Steve Summit | last post by:
-----BEGIN PGP SIGNED MESSAGE----- It's often explained that the reason for some of the imprecision in C's definition is so that C can be implemented on different kinds of machines -- say, those...
5
by: Krumble Bunk | last post by:
Hello! First things first (but not necessarily in that order), this is a really great group, and has helped me understand more and more C everytime I read the postings, so thanks for a great...
9
by: Christopher Weaver | last post by:
I know that the bitwise AND of 8 and 4 will return 0 or false and the bitwise AND of 8 and 9 will return 1 or true but I don't know how to write the synax for it in C#. I have a value that ranges...
37
by: James Radke | last post by:
Hello, I found some code that I could use in my application on the web, but it is written in C#, and I would like to convert it to VB. And I am having problems with one section. Is there...
5
by: noridotjabi | last post by:
I'm learning to program in C and any tutorial or book that I read likes to briefly touch on birdies operators and then move on without giving any sort of example application of them. Call me what...
3
by: Jay Ruyle | last post by:
I'm trying to figure out a way to list several items in a listbox and let the user select any number of items in the listbox. I have tried to code in the items as bitwise items but all it stores...
45
by: Carramba | last post by:
Hi! I now that I can't do straight forward any bitwise operation on float (double etc..). But I wondering what is the easiest/best way to do this? I was thinking if I have float x=1.1111 so I can...
10
by: Rob Wilkerson | last post by:
I'm attempting to do some work around existing code that uses bitwise operations to manage flags passed into a function and I'm quite frankly unequipped to do so. I've never done much with bitwise...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.